老潘已转到http://www.panxingzhi.net/。所有旧文章如有改动,此处将不再更新。谢谢。 I've moved to http://www.panxingzhi.net/. Updates on old posts are not applied here. Thanks.

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 条评论:

添加评论