Geek-Side

Resource > SICP Exersise 2_62


Exersise 2.62
 (define (union-set set1 set2)
    (define (ite set1 set2 result)
      (cond [(null? set1) (append result set2)]
        [(null? set2) (append result set1)]
        [else (let ((x1 (car set1)) (x2 (car set2)))
            (cond [(= x1 x2) (ite (cdr set1) (cdr set2) (append result (list x1)))]
              [(< x1 x2) (ite (cdr set1) set2 (append result (list x1)))]
              [(> x1 x2) (ite set1 (cdr set2) (append result (list x2)))]))]))
    (ite set1 set2 '()))
 
  (use gauche.test)
  (test-start "Exersise 2.62")
  (test* "normal1" '(1 2 3) (union-set '(1) '(2 3)))
  (test* "normal2" '(1 2 3 4) (union-set '(1 2) '(3 4)))
  (test* "normal3" '(1 2 3 4) (union-set '(1 3) '(2 4)))
  (test* "normal4" '(1 2 3 4) (union-set '(4) '(1 2 3)))
  (test* "normal4" '(1 2 3 4) (union-set '(4) '(1 2 3 4)))
  (test-end)