; missing items
(define-syntax fold-of
(syntax-rules (range in is)
((_ "z" f b e) (set! b (f b e)))
((_ "z" f b e (v range fst pst stp) c ...)
(let* ((x fst) (p pst) (s stp)
(le? (if (positive? s) <= >=)))
(do ((v x (+ v s))) ((le? p v) b)
(fold-of "z" f b e c ...))))
((_ "z" f b e (v range fst pst) c ...)
(let* ((x fst) (p pst) (s (if (< x p) 1 -1)))
(fold-of "z" f b e (v range x p s) c ...)))
((_ "z" f b e (v range pst) c ...)
(fold-of "z" f b e (v range 0 pst) c ...))
((_ "z" f b e (x in xs) c ...)
(do ((t xs (cdr t))) ((null? t) b)
(let ((x (car t)))
(fold-of "z" f b e c ...))))
((_ "z" f b e (x is y) c ...)
(let ((x y)) (fold-of "z" f b e c ...)))
((_ "z" f b e p? c ...)
(if p? (fold-of "z" f b e c ...)))
((_ f i e c ...)
(let ((b i)) (fold-of "z" f b e c ...)))))
(define-syntax list-of (syntax-rules ()
((_ arg ...) (reverse (fold-of
(lambda (d a) (cons a d)) '() arg ...)))))
(define (make-hash hash eql? oops size)
(let ((table (make-vector size '())))
(lambda (message . args)
(if (eq? message 'enlist)
(let loop ((k 0) (result '()))
(if (= size k)
result
(loop (+ k 1) (append (vector-ref table k) result))))
(let* ((key (car args))
(index (modulo (hash key) size))
(bucket (vector-ref table index)))
(case message
((lookup fetch get ref recall)
(let loop ((bucket bucket))
(cond ((null? bucket) oops)
((eql? (caar bucket) key) (cdar bucket))
(else (loop (cdr bucket))))))
((insert insert! ins ins! set set! store store! install install!)
(vector-set! table index
(let loop ((bucket bucket))
(cond ((null? bucket)
(list (cons key (cadr args))))
((eql? (caar bucket) key)
(cons (cons key (cadr args)) (cdr bucket)))
(else (cons (car bucket) (loop (cdr bucket))))))))
((delete delete! del del! remove remove!)
(vector-set! table index
(let loop ((bucket bucket))
(cond ((null? bucket) '())
((eql? (caar bucket) key)
(cdr bucket))
(else (cons (car bucket) (loop (cdr bucket))))))))
((update update!)
(vector-set! table index
(let loop ((bucket bucket))
(cond ((null? bucket)
(list (cons key (caddr args))))
((eql? (caar bucket) key)
(cons (cons key ((cadr args) key (cdar bucket))) (cdr bucket)))
(else (cons (car bucket) (loop (cdr bucket))))))))
(else (error 'hash-table "unrecognized message")) ))))))
(define (missing1 xs ys)
(list-of x (x in xs) (not (member x ys))))
(define (missing2 xs ys)
(let loop ((xs (sort xs <)) (ys (sort ys <)) (zs (list)))
(cond ((null? xs) zs)
((< (car xs) (car ys))
(loop (cdr xs) ys (cons (car xs) zs)))
((< (car ys) (car xs))
(loop xs (cdr ys) zs))
(else (loop (cdr xs) (cdr ys) zs)))))
(define (missing3 xs ys)
(let ((h (make-hash (lambda (x) (modulo x 97)) = #f 97)))
(do ((ys ys (cdr ys))) ((null? ys))
(h 'insert (car ys) (car ys)))
(filter (lambda (x) (not (h 'lookup x))) xs)))
(display (missing1 '(5 15 2 20 30 40 8 1) '(2 20 15 30 1 40 0 8))) (newline)
(display (missing2 '(5 15 2 20 30 40 8 1) '(2 20 15 30 1 40 0 8))) (newline)
(display (missing3 '(5 15 2 20 30 40 8 1) '(2 20 15 30 1 40 0 8))) (newline)