I am using this script from The little schemer, to get intersection of two sets. But I am getting unbound identifier error at 'member?', can anyone please tell what's wrong with it:
(define intersect
(lambda (set1 set2)
(cond ((null? set1) (quote ()))
((member? (car set1) set2)
(cons (car setl)
(intersect (cdr set1) set2)))
(else (intersect (cdr setl) set2)))))
I was missing this function above:
(define member?
(lambda (a lat)
(cond ((null? lat) #f)
(else (or (eq? (car lat) a)
(member? a (cdr lat)))))))
Also, I want to intersect two lists like: '((1 2)(2 7)) '((1 3)(4 5)) = '((1 5)), any suggestions on how to go about it? I am looking up the answers from this post: How to write a scheme function that takes two lists and returns four lists
You have a typo in
intersect
where you have switched 1 with as lower case L. If you fix that yourintersect
seems fine by me if you are comparing symbols. Eg.To make it compare other things than symbols, you need to change your
member?
so that it usesequal?
instead ofeq?
. It will be like this:Even after this. The symbol version above still works. In any LISP (Common Lisp and Scheme at least) you have
member
. It usesequal
and evaluate tofalse
(whatever is false in the implementation) when it's not found and if it's found it evaluates to the rest of the argument list starting from where the element was found (which is considered true):Using standard member instead of your own predicate:
EDIT 1
It seems you are not searching for
intersection
but a specialalist
merge:My intersection solution: