Intersection
The intersection (written ∩) of two sets is the set of elements that are members of both sets.
(intersection '(a b c) '(a c e)) -> (c a)
public static Cons intersection (Cons x, Cons y) {
if ( x == null )
return null;
else if ( member(first(x), y) != null )
return
cons(first(x),
intersection(rest(x), y));
else return intersection(rest(x), y); }
(defun intersection (x y)
(if (null x)
nil
(if (member (first x) y)
(cons (first x)
(intersection (rest x) y))
(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.