learning-sicp

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

exercise-61-62.scm (1470B)


      1 (define-library (sicp solutions exercise-2.61-2.62)
      2   (import (scheme base))
      3 
      4   (begin
      5     (define (element-of-set? x set)
      6       (cond
      7        ((= x (car set))
      8         #t)
      9        ((> x (car set))
     10         #f)
     11        (else (element-of-set? x (cdr set)))))
     12 
     13     (define (intersection-set set1 set2)
     14       (cond
     15        ((null? set1) '())
     16        ((null? set2) '())
     17        ((= (car set1)
     18            (car set2))
     19         (cons (car set1)
     20               (intersection-set (cdr set1)
     21                                 (cdr set2))))
     22        ((< (car set1)
     23            (car set2))
     24         (intersection-set (cdr set1)
     25                           set2))
     26        ((> (car set1)
     27            (car set2))
     28         (intersection-set set1
     29                           (cdr set2)))))
     30 
     31     (define (adjoin-set x set)
     32       (cond
     33        ((null? set) (cons x '()))
     34        ((< x (car set)) (cons x set))
     35        ((= x (car set)) set)
     36        (else (cons (car set)
     37                    (adjoin-set x (cdr set))))))
     38 
     39     (define (union-set set1 set2)
     40       (cond
     41        ((null? set1) set2)
     42        ((null? set2) set1)
     43        ((< (car set1)
     44            (car set2))
     45         (cons (car set1)
     46               (union-set (cdr set1)
     47                          set2)))
     48        ((> (car set1)
     49            (car set2))
     50         (cons (car set2)
     51               (union-set set1
     52                          (cdr set2))))
     53        (else (cons (car set1)
     54                    (union-set (cdr set1)
     55                               (cdr set2))))))))