The intersection (written &cap) of two sets is the set of elements that are members of both sets.

(intersection '(a b c) '(a c e))   ->  (c a)

(defun intersection (x y)
  (if (null x)
      (if (member (first x) y)
          (cons (first x)
                (intersection (rest x) y))
          (intersection (rest x) y) ) ) )

public static Cons intersection (Cons x, Cons y) {
  if ( x == null )
     return null;
   else if ( member(first(x), y) != null )
                  intersection(rest(x), y));
         else return intersection(rest(x), y); }

If the sizes of the input lists are m and n, the time required is O(m · n). That is not very good; this version of intersection will only be acceptable for small lists.

Contents    Page-10    Prev    Next    Page+10    Index