intersection of two lists

You have problems, and we're glad to hear them. Explain the problem, what you have tried, and where you got stuck.
Feel free to share a little info on yourself and the course.
Forum rules
Please respect your teacher's guidelines. Homework is a learning tool. If we just post answers, we aren't actually helping. When you post questions, be sure to show what you have tried or what you don't understand.

intersection of two lists

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))`
kiwifreak3

Posts: 5
Joined: Sat Jun 09, 2012 8:23 am

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.

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

nuntius

Posts: 449
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA

Re: intersection of two lists

Thanks a lot nuntius!
works perfectly now!
kiwifreak3

Posts: 5
Joined: Sat Jun 09, 2012 8:23 am

Return to Homework

Who is online

Users browsing this forum: No registered users and 1 guest