Geek-Side

Resource > SICP Exersise 2_61


Exersise 2.61

   (define (adjoin-set x set)
     (define (ite set result)
       (cond [(null? set) (append result (list x))]
         [(= x (car set)) (append result set)]
         [(< x (car set)) (append result (list x) set)]
         [else (ite (cdr set) (append result (list (car set))))]))
     (ite set '()))
  
   (use gauche.test)
   (test-start "Exersise 2.61")
   (test* "normal1" '(1 2 3) (adjoin-set 1 '(2 3)))
   (test* "normal2" '(2 3) (adjoin-set 2 '(2 3)))
   (test* "normal3" '(2 3 4) (adjoin-set 4 '(2 3)))
   (test* "normal4" '(2 4 7) (adjoin-set 4 '(2 7)))
   (test* "null" '(1) (adjoin-set 1 '()))
   (test-end)