```1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 ``` ```; sets (define (make-set limit) (define (hash x) ; universal hash function (define (mod n) (modulo n 4294967296)) (cond ((boolean? x) (if x 1 0)) ((symbol? x) (hash (symbol->string x))) ((char? x) (char->integer x)) ((integer? x) (mod x)) ((real? x) (let* ((r (inexact->exact x)) (n (numerator r)) (d (denominator r))) (mod (+ n (* 37 d))))) ((rational? x) (mod (+ (numerator x) (* 37 (denominator x))))) ((complex? x) (mod (+ (hash (real-part x)) (* 37 (hash (imag-part x)))))) ((null? x) 4294967295) ((pair? x) (let loop ((x x) (s 0)) (if (null? x) s (loop (cdr x) (mod (+ (* 31 s) (hash (car x)))))))) ((vector? x) (let loop ((i (- (vector-length x) 1)) (s 0)) (if (negative? i) s (loop (- i 1) (mod (+ (* 31 s) (hash (vector-ref x i)))))))) ((string? x) (let loop ((i (- (string-length x) 1)) (s 0)) (if (negative? i) s (loop (- i 1) (mod (+ (* 31 s) (hash (string-ref x i)))))))) ((procedure? x) (error 'hash "can't hash procedure")) ((port? x) (error 'hash "can't hash port")) (else (error 'hash "don't know how to hash object")))) (define (member? set key) (let* ((h (modulo (hash key) limit)) (b (vector-ref set h))) (if (member key b) #t #f))) (define (adjoin set key) (let* ((h (modulo (hash key) limit)) (b (vector-ref set h))) (cond ((member key b) set) (else (vector-set! set h (cons key b)) set)))) (define (delete set key) (define (remove x xs) (let loop ((xs xs) (zs (list))) (cond ((null? xs) zs) ((equal? (car xs) x) (append (cdr xs) zs)) (else (loop (cdr xs) (cons (car xs) zs)))))) (let* ((h (modulo (hash key) limit)) (b (vector-ref set h))) (cond ((not (member key b)) set) (else (vector-set! set h (remove key b)) set)))) (define (intersect set1 set2) (let ((set (make-set limit))) (let loop ((keys (enlist set1))) (cond ((null? keys) set) ((set2 'member? (car keys)) (set! set (set 'adjoin (car keys))) (loop (cdr keys))) (else (loop (cdr keys))))))) (define (union set1 set2) (let ((set (make-set limit))) (let loop ((keys (enlist set1))) (when (pair? keys) (set! set (set 'adjoin (car keys))) (loop (cdr keys)))) (let loop ((keys (set2 'enlist))) (when (pair? keys) (set! set (set 'adjoin (car keys))) (loop (cdr keys)))) set)) (define (minus set1 set2) (let ((set (make-set limit))) (let loop ((keys (enlist set1))) (when (pair? keys) (set! set (set 'adjoin (car keys))) (loop (cdr keys)))) (let loop ((keys (set2 'enlist))) (when (pair? keys) (set! set (set 'delete (car keys))) (loop (cdr keys)))) set)) (define (enlist set) (let loop ((i 0) (s (list))) (if (= i limit) s (loop (+ i 1) (append (vector-ref set i) s))))) (define (size set) (let loop ((i 0) (s 0)) (if (= i limit) s (loop (+ i 1) (+ s (length (vector-ref set i))))))) (define (new set) (lambda (message . arg) (dispatch set message arg))) (define (dispatch set message arg) (case message ((member?) (member? set (car arg))) ((adjoin) (new (adjoin set (car arg)))) ((delete) (new (delete set (car arg)))) ((intersect) (intersect set (car arg))) ((union) (union set (car arg))) ((minus) (minus set (car arg))) ((enlist) (enlist set)) ((size) (size set)) (else (error 'set "invalid message")))) (new (make-vector limit (list)))) (define s (make-set 97)) (set! s (s 'adjoin 1)) (set! s (s 'adjoin 2)) (set! s (s 'adjoin 3)) (define t (make-set 97)) (set! t (t 'adjoin 3)) (set! t (t 'adjoin 4)) (set! t (t 'adjoin 5)) (display (s 'enlist)) (newline) (display (s 'size)) (newline) (display (t 'enlist)) (newline) (display (t 'size)) (newline) (display ((s 'intersect t) 'enlist)) (newline) (display ((s 'union t) 'enlist)) (newline) (display ((s 'minus t) 'enlist)) (newline) ```
 ```1 2 3 4 5 6 7 ``` ```(3 2 1) 3 (5 4 3) 3 (3) (5 4 3 2 1) (2 1) ```