Sets in Lisp

Sets can easily be represented as lists of items. The set functions are standard in Common Lisp.

(intersection x y ) returns a list containing the elements that occur in both x and y.


(define (intersection x y)
  (if (pair? x)
      (if (memv (car x) y)
          (cons (car x)
                (intersection (cdr x) y))
          (intersection (cdr x) y))
      '()))

> (intersection '(c a t) '(b a g)) (a)

Contents    Page-10    Prev    Next    Page+10    Index