# weima learns to program

my attempt to do the exercises in sicp.

## Saturday, July 12, 2008

### sicp exercise 2.10

; Exercise 2.10.  Ben Bitdiddle, an expert systems programmer, looks over Alyssa's shoulder and comments that it is not clear what it means to divide by an interval that spans zero. Modify Alyssa's code to check for this condition and to signal an error if it occurs.

(define (mul-interval x y)
(let ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))

(define (make-interval a b) (cons a b))
(define (upper-bound interval) (car interval))
(define (lower-bound interval) (cdr interval))

(define (width interval)
(/ (- (upper-bound interval) (lower-bound interval)) 2))

(define (div-interval x y)
(if (= (width y) 0)
(display "ERROR")
(mul-interval x
(make-interval (/ 1.0 (lower-bound y))
(/ 1.0 (upper-bound y))))))

(display (div-interval (make-interval 2 3) (make-interval 8 8)))