Contents    Page-10    Prev    Next    Page+10    Index   

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)


(defn intersection [x y]
  (if (empty? x)
      '()
      (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.