Anyone looking for solutions should check out one of the many other blogs that have solved this issue, such as Ken Dyck's
or (in progress) Bill the Lizard
That being said, my solution to exercise 2.11 is pretty convoluted, and I'm not too happy with Kens one either... If you have suggestion, comment!
(define (int-type x)
(cond ((> (lower-bound x) 0) 1)
((< (upper-bound x) 0) -1)
(else 0)))
(define (mul-interval x y)
(let ((x-type (int-type x))
(y-type (int-type y)))
(cond ((and (= x-type 1) (= y-type 1))
(make-interval (* (lower-bound x) (lower-bound y))
(* (upper-bound x) (upper-bound y))))
((and (= x-type 1) (= y-type 0))
(make-interval (* (upper-bound x) (lower-bound y))
(* (upper-bound x) (upper-bound y))))
((and (= x-type 1) (= y-type -1))
(make-interval (* (upper-bound x) (lower-bound y))
(* (lower-bound x) (upper-bound y))))
((and (= x-type 0) (= y-type 1))
(make-interval (* (lower-bound x) (upper-bound y))
(* (upper-bound x) (upper-bound y))))
((and (= x-type 0) (= y-type 0))
(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))))
((and (= x-type 0) (= y-type -1))
(make-interval (* (upper-bound x) (lower-bound y))
(* (lower-bound x) (lower-bound y))))
((and (= x-type -1) (= y-type 1))
(make-interval (* (lower-bound x) (upper-bound y))
(* (upper-bound x) (lower-bound y))))
((and (= x-type -1) (= y-type 0))
(make-interval (* (lower-bound x) (upper-bound y))
(* (lower-bound x) (lower-bound y))))
((and (= x-type -1) (= y-type -1))
(make-interval (* (upper-bound x) (upper-bound y))
(* (lower-bound x) (lower-bound y))))
(else (error "This shouldn't happen!")))))
No comments:
Post a Comment