人生最美好的东西,就是他同别人的友谊。——林肯
(define set?
(lambda (l)
(cond
((null? l) #t)
((member? (car l) (cdr l)) #f)
(else (set? (cdr l))))))
(set? ‘(apples peaches pear plums))
(set? ‘(apple peaches apple plum))
(define makesetx
(lambda (lat)
(cond
((null? lat) ‘())
((member? (car lat) (cdr lat)) (makesetx (cdr lat)))
(else (cons (car lat) (makesetx (cdr lat)))))))
(makesetx ‘(apple peach pear peach plum apple lemon peach))
(define makeset
(lambda (lat)
(cond
((null? lat) ‘())
((member? (car lat) (cdr lat)) (cons (car lat) (makeset (multirember (car lat) (cdr lat)))))
(else (cons (car lat) (makeset (cdr lat)))))))
(makeset ‘(apple peach pear peach plum apple lemon peach))
(makeset ‘(apple 3 pear 4 9 apple 3 4))
(define subset?
(lambda (set1 set2)
(cond
((null? set1) #t)
((member? (car set1) set2) (subset? (cdr set1) set2))
(else #f))))
(subset? ‘(4 pounds of horseradish) ‘(four pounds chicken and 5 ounces horseradish))
(subset? ‘(5 chicken wings) ‘(5 hamburgers 2 pieces fried chicken and light duckling wings))
(define subsetx?
(lambda (set1 set2)
(cond
((null? set1) #t)
(else (and (member? (car set1) set2)
(subsetx? (cdr set1) set2))))))
(define eqset?
(lambda (set1 set2)
(cond
((subset? set1 set2)
(subset? set2 set1))
(else #f))))
(define eqsetx?
(lambda (set1 set2)
(and (subsetx? set1 set2) (subsetx? set2 set1))))
(define intersect?
(lambda (set1 set2)
(cond
((null? set1) #f)
(else
(or (member? (car set1) set2)
(intersect? (cdr set1) set2))))))
(intersect? ‘(stewed tomatoes and macaroni) ‘(macaroni and cheese))
(define union
(lambda (set1 set2)
(cond
((null? set1) set2)
((member? (car set1) set2)
(union (cdr set1) set2))
(else (cons (car set1)
(union (cdr set1) set2))))))
版权声明:本文为博主原创文章,未经博主允许不得转载。