SICP-2.9 solution
其实这题应该直接用数学代数式推导,不应该用这种形式。
(define (make-interval a b) (cons a b))
(define (upper-bound i) (cdr i))
(define (lower-bound i) (car i))
(define (add-interval x y)
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
(define (mul-interval x y)
(let ((p1 (* (lower-bound x) (lower-bound y)))
(p2 (* (lower-bound x) (upper-bound y)))
(p3 (* (upper-bound x) (lower-bound y)))
(p4 (* (upper-bound x) (upper-bound y))))
(make-interval (min p1 p2 p3 p4)
(max p1 p2 p3 p4))))
(define (width-interval i)
(/ (-
(upper-bound i)
(lower-bound i))
2))
; for add-interval,
; the width of the result of combining two intervals
; is a function only of the widths of the argument intervals
; HOW TO prove that?
; we used an informal method
(width-interval
(add-interval x y))
; ->
(width-interval
(make-interval (+ (lower-bound x) (lower-bound y))
(+ (upper-bound x) (upper-bound y))))
; ->
(/ (-
(+ (upper-bound x) (upper-bound y))
(+ (lower-bound x) (lower-bound y))) 2)
; ->
(/ (+
(- (upper-bound x) (lower-bound x))
(- (upper-bound y) (lower-bound y))) 2)
; ->
(+
(/ (- (upper-bound x) (lower-bound x)) 2)
(/ (- (upper-bound y) (lower-bound y)) 2))
; ->
(+
(width-interval x)
(width-interval y))
; to show that for mul-interval,
; the width of the result of combining two intervals
; is NOT a function only of the widths of the argument intervals
(width-interval
(mul-interval (make-interval 0 2)
(make-interval 1 3)))
(width-interval
(mul-interval (make-interval 1 3)
(make-interval 1 3)))
0 条评论: