;; A simple linear list search function.
;; Return the first element which satisfies the predicate pred.
;; If no such element is found, return #f.
(define (find-in-list pred lst)
(cond ((null? lst) #f)
((pred (car lst)) (car lst))
(else (find-in-list pred (cdr lst))))) |
| | A linear list search function. A predicate accepts as input an element in the list, and it returns either true (#t) or false (#f).
If the predicate holds (if it returns true), we have found what we searched for.
The predicate pred is passed as the first parameter to find-in-list. As it is emphasized in blue color, the predicate is
applied on the elements of the list. The first successful application (an application with true result) terminates the search,
and the element is returned. If the first case in the conditional succeeds (the brown condition) we have visited all elements in the list,
and we conclude that the element looked for is not there. In that case we return false.
|