sicp每日一題[2.8]

再思即可發表於2024-09-13

Exercise 2.8

> Using reasoning analogous to Alyssa's, describe how the difference of two intervals may be computed. Define a corresponding subtraction procedure, called sub-interval.

這道題目也比較簡單,只要注意到區間之差的下界是被減區間的下界減去另一個區間的上界,上界是被減區間的上界減去另一個區間的下界即可。

; 計算 x-y 的結果
(define (sub-interval x y)
  (make-interval (- (lower-bound x) (upper-bound y))
                 (- (upper-bound x) (lower-bound y))))

(define r1 (make-interval 6.12 7.48))
(define r2 (make-interval 4.465 4.935))

(display (sub-interval r1 r2))

; 執行結果
[1.1850000000000005, 3.0150000000000006]

相關文章