2015年3月29日日曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の2(データによる抽象の構築)、2.1(データ抽象入門)、2.1.4(拡張問題: 区間算術演算)、問題 2.14.を解いてみる。

その他参考書籍

問題 2.14.

コード(BBEdit, Emacs)

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

(define lower-bound
  (lambda (interval) (car interval)))

(define upper-bound
  (lambda (interval) (cdr interval)))

(define print-interval
  (lambda (interval)
    (display "(")
    (display (lower-bound interval))
    (display ", ")
    (display (upper-bound interval))
    (display ")")
    (newline)))

(define interval (make-interval 1 2))

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

(define mul-interval
  (lambda (x y)
    ((lambda (x1 x2 y1 y2)
       (cond ((and (< x2 0) (< y2 0))
              (make-interval (* x2 y2)
                             (* x1 y1)))
             ((and (< x2 0) (< y1 0) (>= y2 0))
              (make-interval (* x1 y2)
                             (* x1 y1)))
             ((and (< x2 0) (>= y1 0))
              (make-interval (* x1 y2)
                             (* x2 y1)))
             ((and (< x1 0) (>= x2 0) (< y2 0))
              (make-interval (* x2 y1)
                             (* x1 y1)))
             ((and (< x1 0) (>= x2 0) (< y1 0) (>= y2 0))
              ((lambda (p1 p2 p3 p4)
                 (make-interval (min p1 p2) (max p3 p4)))
               (* x1 y2) (* x2 y1)
               (* x1 y1) (* x2 y2)))
             ((and (< x1 0) (>= x2 0) (>= y1 0))
              (make-interval (* x1 y2)
                             (* x2 y2)))
             ((and (>= x1 0) (< y2 0))
              (make-interval (* x2 y1)
                             (* x1 y2)))
             ((and (>= x1 0) (< y1 0) (>= y2 0))
              (make-interval (* x2 y1)
                             (* x2 y2)))
             (else
              (make-interval (* x1 y1)
                             (* x2 y2)))))
     (lower-bound x) (upper-bound x)
     (lower-bound y) (upper-bound y))))



(define div-interval
  (lambda (x y)
    (if (<= (* (lower-bound y) (upper-bound y)) 0)
        (error "zero division")
        (mul-interval x
                      (make-interval (/ 1.0 (upper-bound y))
                                     (/ 1.0 (lower-bound y)))))))

(define width
  (lambda (interval)
    (/ (- (upper-bound interval)
          (lower-bound interval))
       2)))

(define add-interval-width
  (lambda (x-width y-width) (+ x-width y-width)))

(define i0 (make-interval 1 1))
(define i1 (make-interval 0 1))
(define i2 (make-interval 1 2))
(define i3 (make-interval -1 1))
(define i4 (make-interval  -2 -1))
(define i5 (make-interval -1 0))
(define intervals (list i0 i1 i2 i3 i4 i5))

(define part1
  (lambda (r1 r2)
    (div-interval (mul-interval r1 r2)
                  (add-interval r1 r2))))

(define part2
  (lambda (r1 r2)
    ((lambda (one)
       (div-interval one
                     (add-interval (div-interval one r1)
                                   (div-interval one r2))))
     (make-interval 1 1))))

(define make-center-percent
  (lambda (center percent)
    (make-interval (- center (abs (* center (/ percent 100))))
                   (+ center (abs (* center (/ percent 100)))))))

(define interval1 (make-interval 10 1.0))
(define interval2 (make-interval 20 2.0))

(print-interval (part1 interval1 interval2))
(print-interval (part2 interval1 interval2))

入出力結果(Terminal(kscheme), REPL(Read, Eval, Print, Loop))

$ gosh sample14.scm
(66.66666666666666, 0.06666666666666667)
(6.666666666666666, 0.6666666666666666)
$

0 コメント:

コメントを投稿