This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

intersection of two lists

3 posts · 9158 views

Hi,
I'm trying to write a recursive function returning the intersection of two given lists... I can't figure out why I'm getting the
error message: void function. I'd be grateful for help!
(defun intersect(A B)
(if (eq A ())
A
(if (member (car A) B) 
(push (car A) (intersect(cdr A) B))
(intersect(cdr A) B))))
(intersect'(1 2 3 4) '(0 1 2))

Re: intersection of two lists

(PUSH obj place) is a macro that uses SETF to modify the place.
There is no SETF expander defined for (intersect ...) forms -- that's the undefined function problem.

Use CONS instead of PUSH, optionally tweak the whitespace, and it all works fine.
(defun intersect  (A B)
  (if (eq A ())
      A
      (if (member (car A) B)
          (cons (car A) (intersect (cdr A) B))
          (intersect (cdr A) B))))

(intersect '(1 2 3 4) '(0 1 2))

Re: intersection of two lists

Thanks a lot nuntius!
works perfectly now!