LISP Problems
RECURSION:

ANOTHER EXAMPLE:

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) 
       (and 
            (not (null y)) 
            (or 
                 (equal x (car y)) 
                 (member x (cdr y))
            )
       )
)

ALTERNATIVELY:

(define member (x y) 
       (cond
             ((null y) nil)
             ((equal x (car y)) t)
             (t (member x (cdr y)))
       )
)
[TOC]

CS1104 Main Page


Last updated 2001/04/06
© J.A.N. Lee, 2001.