SICP exercise 2.07

From Drewiki

Jump to: navigation, search

Problem

Alyssa's interval arithmetic program (see section 2.1.4 of the text) is incomplete because she has not specified the implementation of the interval abstraction. Here is a definition of the interval constructor:

 

Define selectors upper-bound and lower-bound</tt> to complete the implementation.

Solution

Here is a definition of the interval constructor:

 

The implementations of upper-bound and lower-bound are trivial: we know that one will involve cons and the other car. The only issue to decide is which is which.

The definition of add-interval given in the text dictates the order:

 

add-interval expects the order of arguments to the constructor to be first the lower bound, then the upper bound. Here, then, are the definitions of the selectors:

 

Test:

 

Output:

1.3
 

Output:

0.7
Personal tools