Searching Lists - MIT/GNU Scheme 7.7.90+
Description: MIT/GNU Scheme 7.7.90+

Next: Mapping of Lists, Previous: Filtering Lists, Up: Lists

### 7.6 Searching Lists

— procedure: find-matching-item list predicate

> Returns the first element in list for which predicate is true; returns `#f` if it doesn't find such an element. (This means that if predicate is true for `#f`, it may be impossible to distinguish a successful result from an unsuccessful one.) Predicate must be a procedure of one argument.

> For compatibility, `list-search-positive` is an alias for `find-matching-item`. `list-search-negative` is similar but the sense of the predicate is reversed.

— procedure: memq object list
— procedure: memv object list
— procedure: member object list

> These procedures return the first pair of list whose car is object; the returned pair is always one from which list is composed. If object does not occur in list, `#f` (n.b.: not the empty list) is returned. `memq` uses `eq?` to compare object with the elements of list, while `memv` uses `eqv?` and `member` uses `equal?`.1

>               (memq 'a '(a b c))                      =>  (a b c)
>               (memq 'b '(a b c))                      =>  (b c)
>               (memq 'a '(b c d))                      =>  #f
>               (memq (list 'a) '(b (a) c))             =>  #f
>               (member (list 'a) '(b (a) c))           =>  ((a) c)
>               (memq 101 '(100 101 102))               =>  unspecified
>               (memv 101 '(100 101 102))               =>  (101 102)

— procedure: member-procedure predicate

> Returns a procedure similar to `memq`, except that predicate, which must be an equivalence predicate, is used instead of `eq?`. This could be used to define `memv` as follows:

>               (define memv (member-procedure eqv?))

#### Footnotes

[1] Although they are often used as predicates, `memq`, `memv`, and `member` do not have question marks in their names because they return useful values rather than just `#t` or `#f`.