Note: this wiki is now retired and will no longer be updated!

The static final versions of the pages are left as a convenience for readers. Note that meta-pages such as "discussion," "history," etc., will not work.

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 (make-interval a b) (cons a b))

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

Solution

Here is a definition of the interval constructor:

(define (make-interval a b) (cons a b))

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:

(define (add-interval x y)
  (make-interval (+ (lower-bound x) (lower-bound y))
                 (+ (upper-bound x) (upper-bound y))))

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:

(define (lower-bound i) (car i))
 
(define (upper-bound i) (cdr i))

Test:

(upper-bound (make-interval 0.7 1.3))

Output:

1.3
(lower-bound (make-interval 0.7 1.3))

Output:

0.7
Personal tools