Examples of Recursion in Lisp


; Length of a list
(DEFUN LENGTH (L)
  (IF (CONSP L)
      (1+ (LENGTH (REST L)))
      0) )


; Last CONS cell in a list
(DEFUN LAST (L)
  (IF L (IF (REST L)
            (LAST (REST L))
            L)) )


; Membership in a list
(DEFUN MEMBER (X L)
  (IF L (IF (EQL X (FIRST L))
            L
            (MEMBER X (REST L)) )) )

Contents    Page-10    Prev    Next    Page+10    Index