SICP 全笔记

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.

一个区间的上边界和下边界需要判断一次大小才能得到结果

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

(define (upper-bound z)
  (max (car z) (cdr z)))

(define (lower-bound z)
  (min (car z) (cdr z)))

;;; tests begin
(load "../testframe.scm")

(let ((interval1 (make-interval -1 2))
      (interval2 (make-interval 3 2)))
  (begin
    (assert= -1 (lower-bound interval1))
    (assert= 2  (upper-bound interval1))
    (assert= 2  (lower-bound interval2))
    (assert= 3  (upper-bound interval2))))