問題2.62

(define (union-set set1 set2)
  (define (iter set1 set2 product)
    (cond ((null? set1) (append product set2))
          ((null? set2) (append product set1))
          (else (let ((x1 (car set1)) (x2 (car set2)))
                  (cond ((= x1 x2)
                         (iter (cdr set1) (cdr set2)
                               (append product (list x1))))
                        ((< x1 x2)
                         (iter (cdr set1) set2
                               (append product (list x1))))
                        ((< x2 x1)
                         (iter set1 (cdr set2)
                               (append product (list x2)))))))))
  (iter set1 set2 '()))

(union-set '(1 2 5 6) '(3 4 6))
;-> (1 2 3 4 5 6)
(union-set '(1 2 6) '(3 4 5))
;-> (1 2 3 4 5 6)
(union-set '(1 2) '(3 4 5))
;-> (1 2 3 4 5)
(union-set '(1 2 4 16) '(3 4 8))
;-> (1 2 3 4 8 16)