; subset sum
(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 (identity x) x)
(define (subset-sum xs t)
(let ((h (make-hash identity = #f 99991)))
(let loop1 ((xs xs))
(if (null? xs) #f
(let loop2 ((ys (cons (cons 0 (list)) (h 'enlist))))
(cond ((null? ys) (loop1 (cdr xs)))
((= (+ (car xs) (caar ys)) t)
(cons (car xs) (cdar ys)))
(else (h 'insert (+ (car xs) (caar ys))
(cons (car xs) (cdar ys)))
(loop2 (cdr ys)))))))))
(display (subset-sum '(267 439 869 961 1000 1153 1246 1598 1766 1922) 5842))