learning-sicp

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

exercise-7.scm (1391B)


      1 (define-library (sicp solutions chapter-2 exercise-7)
      2   (import (scheme base)
      3           (srfi :42))
      4   (export
      5    add-interval
      6    lower-bound
      7    make-interval
      8    upper-bound
      9    width-interval
     10    )
     11 
     12   (begin
     13     (define (make-interval lower higher)
     14       (cons lower higher))
     15     (define (lower-bound interval)
     16       (car interval))
     17     (define (upper-bound interval)
     18       (cdr interval))
     19 
     20     (define (add-interval x y)
     21       (make-interval (+ (lower-bound x)
     22                         (lower-bound y))
     23                      (+ (upper-bound x)
     24                         (upper-bound y))))
     25 
     26     (define (mul-interval x y)
     27       (let* ((bound-getters (list lower-bound higher-bound))
     28              (ps (list-ec (: x-bound bound-getters)
     29                           (: y-bound bound-getters)
     30                           (* (x-bound x)
     31                              (y-bound y)))))
     32         (make-interval (min ps)
     33                        (max ps))))
     34 
     35     (define (div-interval x y)
     36       (mul-interval x (make-interval (/ 1.0 (upper-bound y))
     37                                      (/ 1.0 (lower-bound y)))))
     38 
     39     (define (sub-interval x y)
     40       (make-interval (- (lower-bound x)
     41                         (lower-bound y))
     42                      (- (upper-bound x)
     43                         (upper-bound y))))
     44 
     45     (define (width-interval x)
     46       (/ (- (upper-bound x)
     47             (lower-bound x))
     48          2))))