2014年3月12日水曜日

開発環境

計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の2(データによる抽象の構築)、2.1(データ抽象入門)、2.1.4(拡張問題: 区間算術演算)、問題 2.10.を解いてみる。

その他参考書籍

問題 2.10.

コード(BBEdit, Emacs)

sample.scm

#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-

;; これまでに書いた手続き
(load "./procedures.scm")

;; 2つの区間の商
(define (div-interval x y)
  (if (= (width y) 0)
      (error "区間0除算")
      (mul-interval x
                    (make-interval (/ 1.0
                                      (upper-bound y))
                                   (/ 1.0
                                      (lower-bound y))))))

(define i1 (make-interval 0 5))
(define i2 (make-interval 5 10))
(define i3 (make-interval 0 0))
(define i4 (make-interval 5 5))
(define i5 (make-interval 10 10))

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

gosh> (load "./sample")
#t
gosh> (div-interval i1 i1)
(0.0 . +inf.0)
gosh> (div-interval i1 i2)
(0.0 . 1.0)
gosh> (div-interval i1 i3)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i1 i4)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i1 i5)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i2 i1)
(1.0 . +inf.0)
gosh> (div-interval i2 i2)
(0.5 . 2.0)
gosh> (div-interval i2 i3)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i2 i4)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i2 i5)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i3 i1)
(0.0 . 0.0)
gosh> (div-interval i3 i2)
(0.0 . 0.0)
gosh> (div-interval i3 i3)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i3 i4)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i3 i5)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i4 i1)
(1.0 . +inf.0)
gosh> (div-interval i4 i2)
(0.5 . 1.0)
gosh> (div-interval i4 i3)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i4 i4)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i4 i5)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i5 i1)
(2.0 . +inf.0)
gosh> (div-interval i5 i2)
(1.0 . 2.0)
gosh> (div-interval i5 i3)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i5 i4)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> (div-interval i5 i5)
*** ERROR: 区間0除算
Stack Trace:
_______________________________________
gosh> 

0 コメント:

コメントを投稿