exercise-64.scm (2079B)
1 2 (define (ordered-list-set-stuff) 3 (define (element-of-set? x set) 4 (cond 5 ((= x (car set)) 6 #t) 7 ((> x (car set)) 8 #f) 9 (else (element-of-set? x (cdr set))))) 10 11 (define (intersection-set set1 set2) 12 (cond 13 ((null? set1) '()) 14 ((null? set2) '()) 15 ((= (car set1) 16 (car set2)) 17 (cons (car set1) 18 (intersection-set (cdr set1) 19 (cdr set2)))) 20 ((< (car set1) 21 (car set2)) 22 (intersection-set (cdr set1) 23 set2)) 24 ((> (car set1) 25 (car set2)) 26 (intersection-set set1 27 (cdr set2))))) 28 29 (define (adjoin-set x set) 30 (cond 31 ((null? set) (cons x '())) 32 ((< x (car set)) (cons x set)) 33 ((= x (car set)) set) 34 (else (cons (car set) 35 (adjoin-set x (cdr set)))))) 36 37 (define (union-set set1 set2) 38 (cond 39 ((null? set1) set2) 40 ((null? set2) set1) 41 ((< (car set1) 42 (car set2)) 43 (cons (car set1) 44 (union-set (cdr set1) 45 set2))) 46 ((> (car set1) 47 (car set2)) 48 (cons (car set2) 49 (union-set set1 50 (cdr set2)))) 51 (else (cons (car set1) 52 (union-set (cdr set1) 53 (cdr set2)))))) 54 (test-begin "2.61") 55 (test-equal 56 '(1) 57 (adjoin-set 1 '())) 58 (test-equal 59 '(1) 60 (adjoin-set 1 '(1))) 61 (test-equal 62 '(1 2) 63 (adjoin-set 1 '(2))) 64 (test-equal 65 '(1 2) 66 (adjoin-set 2 '(1))) 67 (test-equal 68 '(1 2 3 4 5 6) 69 (adjoin-set 3 '(1 2 4 5 6))) 70 (test-end "2.61") 71 72 (test-begin "2.62") 73 (test-equal 74 '() 75 (union-set '() '())) 76 (test-equal 77 '(1) 78 (union-set '(1) '())) 79 (test-equal 80 '(1) 81 (union-set '() '(1))) 82 (test-equal 83 '(1) 84 (union-set '(1) '(1))) 85 (test-equal 86 '(1 2) 87 (union-set '(1 2) '(1))) 88 (test-equal 89 '(1 2) 90 (union-set '(1) '(1 2))) 91 (test-equal 92 '(1 2 3) 93 (union-set '(2 3) '(1 2 3))) 94 (test-end "2.62")) 95 96 (ordered-list-set-stuff)