sicp每日一题[2.7]
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 selector supper-bound and lower-bound to complete the implementation.
这道题目难度不大,比起前面的选择函数,就是多了一个取最小值为下界,最大值为上界的步骤,直接利用现成的 min, max 即可。
; x 表示某个区间
(define (lower-bound x) (min (car x) (cdr x)))
(define (upper-bound x) (max (car x) (cdr x)))
(define helper (make-interval 1.0 1.0))
(define r1 (make-interval 6.12 7.48))
(define r2 (make-interval 4.465 4.935))
; 计算并联电路的电阻,注意这里不能化简成 r1*r2/(r1+r2) 的形式
(define (parallel-resistance r1 r2)
(div-interval helper (add-interval (div-interval helper r1)
(div-interval helper r2))))
(display (parallel-resistance r1 r2))
; 执行结果
[2.581558809636278, 2.97332259363673]