VII. Examples

MEMBER

Given an atom A and a list of atoms LAT, determine whether A occurs in LAT:
(DEFUN MEMBER (A LAT)
	(COND
	((NULL LAT)   NIL)
	((EQ A (CAR LAT))   T)
	(T   (MEMBER A (CDR LAT))) ) )
(Actually, MEMBER is already defined in LISP. Older LISP systems will let you redefine built-in functions, and will use your function in preference to the built-in version. The only Common LISP system that I have used will let you redefine built-in functions, but then gets horribly confused. In any case, redefining built-in functions is generally a bad idea.)

UNION

Define a set to be a list of atoms, such that no atom is ever repeated, and the order of atoms does not matter. The following function computes the union of two sets, that is, a third set containing any atom found in either or both of the two given sets.

(DEFUN UNION (SET1 SET2)
	(COND
	((NULL SET1)   SET2)
	((MEMBER (CAR SET1) SET2)   (UNION (CDR SET1) SET2))
	(T   (CONS (CAR SET1) (UNION (CDR SET1) SET2))) ) ) )
Previous page First page Next page

Copyright © 1995 by David Matuszek
All rights reserved.
Last updated July 15, 1995