I have 2 lists of elements '(a b c) '(d b f) and want to find differences, union, and intersection in one result. Is that possible? How? I wrote a member function that checks if there is a car of the first list in the second list, but I can't throw a member to the new list.
(define (checkResult lis1 lis2) (cond. )) (checkresult '( a b c) '(d b f))
My result should be (( a c) (d f) (a b c d f) (b)) .
29.5k 3 3 gold badges 68 68 silver badges 112 112 bronze badges
asked Apr 19, 2009 at 14:22
merci merci
Homework? YOu should add a 'homework' tag if so.
Commented Apr 19, 2009 at 14:30
Like others have said, all you need to do is create separate functions to compute the intersection, union, and subtraction of the two sets, and call them from checkresult:
(define (checkresult a b) (list (subtract a b) (subtract b a) (union a b) (intersect a b)))
Here are some example union, intersection, and subtraction functions:
(define (element? x lst) (cond ((null? lst) #f) ((eq? x (car lst)) #t) (#t (element? x (cdr lst))))) (define (union a b) (cond ((null? b) a) ((element? (car b) a) (union a (cdr b))) (#t (union (cons (car b) a) (cdr b))))) (define (intersect a b) (if (null? a) '() (let ((included (element? (car a) b))) (if (null? (cdr a)) (if included a '()) (if included (cons (car a) (intersect (cdr a) b)) (intersect (cdr a) b)))))) (define (subtract a b) (cond ((null? a) '()) ((element? (car a) b) (subtract (cdr a) b)) (#t (cons (car a) (subtract (cdr a) b)))))
Note: since these are sets and order doesn't matter, the results are not sorted. Also, the functions assume that the inputs are sets, and therefore don't do any duplicate checking beyond what's required for union.