learning-sicp

My embarrassing half assed SICP run.
git clone https://kaka.farm/~git/learning-sicp
Log | Files | Refs

exercise-59.scm (1087B)


      1 
      2 (define (sets-stuff)
      3   (define (element-of-set? x set)
      4     (cond
      5      ((null? set) #f)
      6      ((equal? x (car set)) #t)
      7      (else (element-of-set? x (cdr set)))))
      8 
      9   (define (adjoin-set x set)
     10     (if (element-of-set? x set)
     11         set
     12         (cons x set)))
     13 
     14   (define (intersection-set set1 set2)
     15     (cond
     16      ((or (null? set1)
     17           (null? set2))
     18       '())
     19      ((element-of-set? (car set1)
     20                        set2)
     21       (adjoin-set (car set1)
     22                   (intersection-set (cdr set1)
     23                                     set2)))
     24      (else (intersection-set (cdr set1)
     25                              set2))))
     26 
     27   (define (union-set set1 set2)
     28     (cond
     29      ((null? set1) set2)
     30      ((null? set2) set1)
     31      ((element-of-set? (car set1)
     32                        set2)
     33       (union-set (cdr set1)
     34                  set2))
     35      (else (adjoin-set (car set1)
     36                        (union-set (cdr set1)
     37                                   set2)))))
     38 
     39   (test-begin "2.59")
     40   (test-equal
     41       '(1 2 4 3 5)
     42     (union-set '(1 3 2) '(4 3 5)))
     43   (test-end "2.59"))
     44 
     45 (sets-stuff)
     46