; File: intersection.scm
; Title: Intersection of two sets (sets as lists)
; recursive, with for-each, with do
; Ex: (intersection '(a b c) '(c d a)) -> (a c)
(define intersection (lambda (x y)
(cond ((null? x ) '())
((member (car x ) y) (cons (car x) (intersection (cdr x ) y)))
(#t (intersection (cdr x ) y))
)))
; Ex: > (intersection2 '(m a b c d) ' (n c d f b )) -> (d c b)
(define intersection2 (lambda (x y)
(let ( (res ()) )
(for-each (lambda(i) (if (member i y) (set! res (cons i res))) ) X )
res
)))
; iterative with do
(define intersection1 (lambda (x y)
(do ( (i x (cdr i)) (res ())) ((null? i) res )
(if (member (car i) y) (set! res (cons (car i) res)))
)
))