SICP 2.1.4 Exercises

March 27, 2014 11:21

This section is marked as an ‘Extended Exercise’, which means an in-depth exploration of a particular system. These will include the ideas presented in the previous section, but with a focus on more practical applications. In this case, we want a system for dealing with interval arithmetic. What that means in relation to this section is that we’re going to develop a data type for intervals, as well as the functions required to properly work with it.

As this system is mathematical in nature, it’s fairly easy to test as there is a definite correct result. We are dealing with a whole domain of numbers, though, and thus testing has to be restricted to a small number of cases that, if correct, imply the whole system ought to work. Even though the system does have correct, easily testable results, it is not always obvious or intuitive as with regular arithmetic, as is shown in several of the problems.

Exercise 2.11 specifically asks for figuring out the set of cases that need to be considered. The tests cover each of those separately (and so serve as a hint to the problem). It’s almost always worthwhile to consider 0 as a special case, since many errors of thought can be discovered in that way, and so several extra tests include 0 (and Exercise 2.10 is an explicit example of a problem with 0.) Each of these values is fixed in the code. By contrast, in Exercise 2.13, random values are used for testing and won’t necessarily have the same values with each run. This works as it is more of an examination of the idea instead of a problem with a single correct answer. Certainly random values could also be used in the tests for 2.11, with a bit more work to ensure that they fit the desired domain, but that’s still no guarantee that the tests will predict all possible behavior. Considered as a mathematical problem, the specific values are sufficient, but it is possible to imagine that the lower-level implementation of the numbers (or even the hardware) could yield unexpected results for very specific values.

The last question of this section is a very open-ended one, and could lead to quite a lot of additional effort. I’ll mention it a bit in the solutions, but suffice it to say that since this book was originally written, there have been methods developed that address this problem.

Exercises 2.1.4