SICP 2.7~2.16練習問題の答え


この段落のテーマは少し多くて、すべて前のコードがあって、いっしょに書くことができます
2.13代数式で見ればわかりますが、比較的簡単です.
2.14コードで試してみるとわかりますが、操作に誤差が生じるような気がしますが、なぜか、2.15、2.16と同じです
しかし2.14の結果から,2.15の結論は正しいはずであり,除算演算に新たな誤差は導入されていないことが分かった.
2.2節から楽しいデータ構造に入りました^^.楽しみです.
(define (make-interval a b) (cons a b))

;; 2.7
(define (lower-bound n) (car n))
(define (upper-bound n) (cdr n))

(define (width-interval i)
  (/ (- (upper-bound i) (lower-bound i)) 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))))

;; 2.11
(define (mul-interval x y)
  (let ((x-l (lower-bound x))
        (x-u (upper-bound x))
        (y-l (lower-bound y))
        (y-u (upper-bound y)))
    (cond ((and (> 0 x-l) (> 0 x-u)) (cond ((and (> 0 y-l) (> 0 y-u)) (make-interval (* x-l y-l) (* x-u y-u)))
                                           ((and (> 0 y-l) (< 0 y-u)) (make-interval (* x-l y-u) (* x-l y-l)))
                                           ((and (< 0 y-l) (< 0 y-u)) (make-interval (* x-l y-u) (* x-u y-l)))))
          ((and (> 0 x-l) (< 0 x-u)) (cond ((and (> 0 y-l) (> 0 y-u)) (make-interval (* x-u y-l) (* x-l y-l)))
                                           ((and (> 0 y-l) (< 0 y-u)) (make-interval (* x-l y-u) (* x-u y-u)))
                                           ((and (< 0 y-l) (< 0 y-u)) (make-interval (* x-l y-u) (* x-u y-u)))))
          ((and (< 0 x-l) (< 0 x-u)) (cond ((and (> 0 y-l) (> 0 y-u)) (make-interval (* x-u y-l) (* x-l y-u)))
                                           ((and (> 0 y-l) (< 0 y-u)) (make-interval (* x-l y-u) (* x-u y-u)))
                                           ((and (< 0 y-l) (< 0 y-u)) (make-interval (* x-l y-l) (* x-u y-u))))))))

(define (div-interval x y)
  ;; 2.10
  (if (> 0 (* (upper-bound y) (lower-bound y)))
      (error "divisor interval should be above 0")
      (mul-interval x
                    (make-interval (/ 1.0 (upper-bound y))
                                   (/ 1.0 (lower-bound y))))))

;; 2.12
(define (make-center-width c w)
  (make-interval (- c w) (+ c w)))

(define (make-center-percent center percent)
  (let ((width (* center ( / percent 100.0))))
    (make-center-width center width)))
 
(define (percent i)
  (* (/ (interval-width i) (center i)) 100.0))

;; 2.8
(define (sub-interval a b)
  (make-interval (- (lower-bound a) (upper-bound b))
                 (- (upper-bound a) (lower-bound b))))

(define (par1 r1 r2)
  (div-interval (mul-interval r1 r2)
                (add-interval r1 r2)))

(define (par2 r1 r2)
  (let ((one (make-interval 1 1)))
    (div-interval one
                  (add-interval (div-interval one r1)
                                (div-interval one r2)))))