Define a function that determines whether (i.e. true or false) a given item (x) is contained in a given list (y).
Solution in pseudocode:
- Provided that the list y is not an empty list then
- If x is the head item in the list y or x is in the tail of the list y then true
- else false
Note the same basic form as in the previous example - check the head item first and then work on the tail.
(define member (x y) (and (not (null y)) (or (equal x (car y)) (member x (cdr y)) ) ) )
(define member (x y) (cond ((null y) nil) ((equal x (car y)) t) (t (member x (cdr y))) ) )
CS1104 Main Page
Last updated 2001/04/06
© J.A.N. Lee, 2001.