我想创建一个函数,它将参数作为参数两个列表,如果它们相交则返回#t
(具有共同的任何元素)。
(define member?
(lambda (a lat)
(cond
((null? lat) #f)
(else (or (equal? (car lat) a)
(member? a (cdr lat)))))))
(define intersect
(lambda (set1 set2)
(cond
((null? set1)(quote ()))
((member? (car set1) set2)
(cons (car set1)
(intersect (cdr set1) set2)))
(else (intersect (cdr set1) set2)))))
(intersect '(1 2) '(2 4)) ;
答案 0 :(得分:1)
您想查看sets in the documentation。与它们相交可以写成:
(define (intersect? lst1 lst2)
(let ((set1 (list->set lst1))
(set2 (list->set lst2)))
(set=? set1
(set-intersect set1
set2))))
(intersect? '(a b) '(b a d)) ; ==> #t
(intersect? '(b a d) '(a b)) ; ==> #f
答案 1 :(得分:0)