Exercise2-6 <---> Exercise2-8

Exercise 2.7.

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.


Ru: Программа Лизы неполна, поскольку она не определила, как реализуется абстракция интервала. Вот определение конструктора интервала:

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

Завершите реализацию, определив селекторы upper-bound и lower-bound.


Scheme solution:

It's a bit unclear what is smaller - a or b in this execrice, but we can figure out this from the text of chapter: a is smaller. Anyway, we can allow both variants using the following selectors:

(define (lower-bound interval) 
  (min (car interval) (cdr interval)))
(define (upper-bound interval) 
  (max (car interval) (cdr interval)))

But then it's better to implement correct order in constructor to simpluft selectors:

(define (make-interval a b) 
   (cons (min a b) (max a b)))
(define (lower-bound interval) (car interval))
(define (upper-bound interval) (cdr interval))

Exercise2-6 <---> Exercise2-8


Comments

The answer above is incorrect. A and B are not end-points to an interval but each in itself is an interval. The add-interval function is adding two different intervals, each of which has a lower bound point and an upper bound point.

The correct answer is: (define (upper-bound x) (cadr x)) (define (lower-bound x) (car x))

I might be wrong in my interpretation, would love to hear feedback.

Posted by 76-14-65-64 at 2009-01-11 05:06:44 X

A and B are indeed intervals (each of them a pair with upper/lower bounds), but to extract the lower/upper bound, you definitely need min/max. Exercise didn't specify which of a or b is larger- (a & b comprises intervals A or B) so the solution above is correct.

Posted by 195 at 2009-02-18 08:04:39 X

Erm no, a and b are not intervals. They are end-points.

We are taking two values like 1 and 2 and making an interval (1,2). You can see this is the case from the procedures given in the text.

Posted by 204 at 2009-06-24 12:37:11 X

:) :)) :( ;) :\ |) X-( B) Markup

Exercise2-7 (last edited 2008-05-11 11:36:09 by localhost)