<< Previous exercise (2.10) | Index | Next exercise (2.12) >>

Yeah, Ben certainly has an evil streak.

I was able to reduce a bit of the clutter by reworking the way the conditional logic is framed. I observed that there were certain patterns in where values were getting passed to the call to make-interval. So rather than working through the conditions and calling make-interval accordingly, I set it up to select the appropriate value for each "slot".

For clarity everything else is the same as jz's solution above. The testing procedures he came up with were also invaluable in working out kinks. Ultimately I'm not sure my solution is any clearer or better -- it's quite possibly neither. It is a tiny bit more concise though:

```
(define (mul-interval x y)
(define (endpoint-sign i)
(cond ((and (>= (upper-bound i) 0)
(>= (lower-bound i) 0))
1)
((and (< (upper-bound i) 0)
(< (lower-bound i) 0))
-1)
(else 0)))
(let ((es-x (endpoint-sign x))
(es-y (endpoint-sign y))
(x-up (upper-bound x))
(x-lo (lower-bound x))
(y-up (upper-bound y))
(y-lo (lower-bound y)))
(if (and (= es-x 0) (= es-y 0))
; Take care of the exceptional condition where we have to test
(make-interval (min (* x-lo y-up) (* x-up y-lo))
(max (* x-lo y-lo) (* x-up y-up)))
; Otherwise, select which value goes in which "slot". I'm not sure
; whether there is an intuitive way to explain *why* these
; selections work.
(let ((a1 (if (and (<= es-y 0) (<= (- es-y es-x) 0)) x-up x-lo))
(a2 (if (and (<= es-x 0) (<= (- es-x es-y) 0)) y-up y-lo))
(b1 (if (and (<= es-y 0) (<= (+ es-y es-x) 0)) x-lo x-up))
(b2 (if (and (<= es-x 0) (<= (+ es-x es-y) 0)) y-lo y-up)))
(make-interval (* a1 a2) (* b1 b2))))))
```

This solutions has 9 cases, exactly as it is required by the problem statement, and all of them are clearly visible (although I wouldn't say readable). I had to redefine positive? predicate that is provided by my scheme interpreter. It also passes all of jz's test cases.

```
(define (mul-interval x y)
(define (positive? x) (>= x 0))
(define (negative? x) (< x 0))
(let ((xl (lower-bound x))
(xu (upper-bound x))
(yl (lower-bound y))
(yu (upper-bound y)))
(cond ((and (positive? xl) (positive? yl))
(make-interval (* xl yl) (* xu yu)))
((and (positive? xl) (negative? yl))
(make-interval (* xu yl) (* (if (negative? yu) xl xu) yu)))
((and (negative? xl) (positive? yl))
(make-interval (* xl yu) (* xu (if (negative? xu) yl yu))))
((and (positive? xu) (positive? yu))
(let ((l (min (* xl yu) (* xu yl)))
(u (max (* xl yl) (* xu yu))))
(make-interval l u)))
((and (positive? xu) (negative? yu))
(make-interval (* xu yl) (* xl yl)))
((and (negative? xu) (positive? yu))
(make-interval (* xl yu) (* xl yl)))
(else
(make-interval (* xu yu) (* xl yl))))))
```

I believe jz's tests go beyond the scope of this problem by allowing an interval's lower bound to be greater than its upper bound and can make this problem seem more difficult than it actually is.

The constructor for make-interval given in the book (2nd edition) does not swap the values if the first parameter is greater than the second parameter. Instead the onus is on the client to use the function properly. The client should assume that the lower bound has to be smaller than the upper bound. jz's tests while all encompassing will not pass vpraid's solution unless you alter the make-interval constructor to swap out of order values.

Ben Bitdiddle should stick to diddling his own bits. His comment just obfuscates the code. But, if we know the signs of the endpoints, we have 3 possible cases for each interval: both ends positive, both negative, or an interval spanning zero; therefore, there are 3x3 = 9 possible cases to test for, which reduces the number of multiplications, except when both intervals span zero.

The above is so gross I tested it out. Yes, I'm a keener.