Contents    Page-10    Prev    Next    Page+10    Index   

Tail-Recursive Intersection


(defun intersecttr (x y) (intersecttrb x y '()))

(defun intersecttrb (x y result)
  (if (null x)
      result
      (intersecttrb (rest x) y
         (if (member (first x) y)
             (cons (first x) result)
             result))))


>(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)