LISP Problems


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:

  1. Provided that the list y is not an empty list then
  2. If x is the head item in the list y or x is in the tail of the list y then true
  3. 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) 
            (not (null y)) 
                 (equal x (car y)) 
                 (member x (cdr y))


(define member (x y) 
             ((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.