SICP Exercise 2.7
Question
Alyssa’s program 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
to complete the
implementation.
Answer
Since it doesn’t specify an order for the two parameters in the question, I’m assuming we have to work out for ourselves which of the two bounds is the lower one. The following implementation should work:
(define (lower-bound i)
(min (car i) (cdr i)))
(define (upper-bound i)
(max (car i) (cdr i)))
We can now set up some test data and see if this does what it is supposed to.
(define i1 (make-interval 1 3))
(define i2 (make-interval 5 2))
(lower-bound i1)
(upper-bound i1)
(lower-bound i2)
(upper-bound i2)
Results:
1
3
2
5