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.
There were many blog posts which talks about Exercise 2.10, and I’ve found that there were 2 ways of understanding of the problem on the internet.
- The problem literally tells us to implement an error that occurs when we pass two arguments that spans zero (like -5 and 3).
- The problem tells us to implement errors by zero division.
So, which one is correct? After a straightforward survey, I’ve found that the answer seems to be the former one.
Then why people feel confused to understand the meaning of the problem?
At a first glance, not a few people might have thought that “OK, this is an problem that related to the procedure div-interval, right?”, then tried to pass arguments for div-interval like this:
(define i1 (make-interval 5 5)) (define i2 (make-interval -5 5)) (div-interval i1 i2) ; (-1.0 . 1.0)
The output seems to work correct ( in fact it does, because the procedure mul-interval properly extracts the result from p1 ~ p4, using the procedures min & mul ). Now we don’t know what the actual problem is.
The answer is simple. What we really had to check was not the procedure div-interval but the following process:
(make-interval (/ 1.0 (upper-bound x)) (/ 1.0 (lower-bound x)))
Let’s check the behavior of this process.
(define i (make-interval -5 5)) (make-interval (/ 1.0 (upper-bound i)) (/ 1.0 (lower-bound i))) ; (0.2, -0.2)
The output is (0.2, -0.2), and now we know what we have to do is:
(define (reciprocal-interval x)
(if (< (* (upper-bound x) (lower-bound x)) 0) (error "The arguments spans 0") (make-interval (/ 1.0 (upper-bound x)) (/ 1.0 (lower-bound x))))) [/shell] I think this is the true understanding of the exercise 2.10. As long as we use the reciprocal-value for only the procedure div-interval, it works well, so we don't need to implement the error function. However, say we want to implement another procedure using the reciprocal interval --- the tragedy occurs. So we have to test the black-box thoroughly even if one of abstract procedures that using the black-box seems to work fine. They can sometimes hide the flaw of the implementation and make problem difficult, like this exercise.