Page 1 of 1

intersection of two lists

Posted: Wed Aug 01, 2012 8:29 am
by kiwifreak3
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!

Code: Select all

(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

Posted: Wed Aug 01, 2012 9:38 am
by nuntius
(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.

Code: Select all

(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

Posted: Wed Aug 01, 2012 1:42 pm
by kiwifreak3
Thanks a lot nuntius!
works perfectly now!