Contents    Page-10    Prev    Next    Page+10    Index   

Tail-Recursive Intersection


(defn intersecttrb [x y answer]
  (if (empty? x)
      answer
      (intersecttrb (rest x) y
         (if (member (first x) y)
             (cons (first x) answer)
             answer))))

(defn intersecttr [x y] (intersecttrb x y '()))


>(intersecttr '(a b c) '(a c e))

  1> (INTERSECTTR (A B C) (A C E))
    2> (INTERSECTTRB (A B C) (A C E) NIL)
      3> (INTERSECTTRB (B C) (A C E) (A))
        4> (INTERSECTTRB (C) (A C E) (A))
          5> (INTERSECTTRB NIL (A C E) (C A))
          <5 (INTERSECTTRB (C A))
        <4 (INTERSECTTRB (C A))
      <3 (INTERSECTTRB (C A))
    <2 (INTERSECTTRB (C A))
  <1 (INTERSECTTR (C A))
(C A)