問題2.7、問題2.8、問題2.9 – SICP(計算機プログラムの構造と解釈)その30

問題2.7

2つの区間の演算

(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 (div-interval x y)
  (mul-interval x
                (make-interval (/ 1.0 (upper-bound y))
                               (/ 1.0 (lower-bound y)))))

区間構成子

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

選択肢 upper-bound

(define (upper-bound x)
  (cdr x))

選択肢 lower-bound

(define (lower-bound x)
  (car x))

問題2.8

2つの区間の差

2つの区間の和と差

2つの区間の差は2つの区間の差が最小になるものと、最大になるものからなる。

(define (sub-interval x y)
  (make-interval (- (lower-bound y) (upper-bound x))
                 (- (upper-bound y) (lower-bound x))))

(define small (make-interval 2 4))
(define big (make-interval 9 15))
(add-interval small big)
gosh> (11 . 19)
(sub-interval small big)
gosh> (5 . 13)
(mul-interval small big)
gosh> (18 . 60)
(div-interval small big)
gosh> (0.13333333333333333 . 0.4444444444444444)

問題2.9

区間a
  • 幅:Wa
  • 区間aの下限:a-Wa
  • 区間aの上限:a+Wa
区間b
  • 幅:Wb
  • 区間bの下限:b-Wb
  • 区間bの上限:b+Wb

区間aと区間bの幅

(add-interval a b)

   (a-Wa)+(b-Wb)    (a+b)-(Wa+Wb)
対 ------------- => -------------
   (a+Wa)+(b+Wb)    (a+b)+(Wa+Wb)

(sub-interval a b)

   (b-Wb)-(a+Wa)    (b-a)-(Wb+Wa)
対 ------------- => -------------
   (b+Wb)-(a-Wa)    (b-a)+(Wb+Wa)

区間aの幅を返す手続き

(define (width-of-interval a)
  (/ (- (upper-bound a) (lower-bound a)) 2.0))

(define small (make-interval 2 4))
(define big (make-interval 9 15))
(width-of-interval small)
gosh> 1.0
(width-of-interval big)
gosh> 3.0
(width-of-interval (add-interval small big))
gosh> 4.0
(width-of-interval (sub-interval small big))
gosh> 4.0
(width-of-interval (mul-interval small big))
gosh> 21.0
(width-of-interval (div-interval small big))
gosh> 0.15555555555555556
計算機プログラムの構造と解釈
ジェラルド・ジェイ サスマン ジュリー サスマン ハロルド エイブルソン
ピアソンエデュケーション
売り上げランキング: 6542
«
»