Recursive Function Definitions
(the basic looping methodology)
(define append (x y)
( (= '() x) (cons y '()))
(t (cons (car x) (append (cdr x) y)))
where "t" is the predefined constant with the value "true". This is equivalent to an "otherwise" condition.
For "safety" every cond statement should have an otherwise
Click here to evaluate the expression
(append '(A B) 'C)
The basic methodology of implementing LISP functions operating over lists using recursion is to operate on the CAR item non-recursively, and then over the CDR recursively. Look again at the above function definition to see this methodology. Here is another:
Given an atom A and a list of atoms LAT, determine whether A occurs in LAT:
(define MEMBER (A LAT) (cond ((null LAT) nil) ((eq A (car LAT)) t) (t (MEMBER A (cdr LAT))) ) )
Reserved words (such as define, cdr, null, etc.) must be in lower case in our version; parameters or literals may be in upper or lower case, but are not interchangeable. That is (eq 'A 'a) will return nil (false).
** This must also preclude indirect recursion where a cycle of function calls eventually returns to the original function.
Last Updated 03/22/2000
© L.Heath, 2000, modified by J.A.N. Lee.