開発環境
- OS X Lion - Apple(OS)
- Emacs、BBEdit - Bare Bones Software, Inc. (Text Editor)
- プログラミング言語: MIT/GNU Scheme
計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション)の2(データによる抽象の構築)、2.1(データ抽象入門)、2.1.4(拡張問題: 区間算術演算)の問題 2.7、問題 2.8、問題 2.9を解いてみる。
その他参考書籍
問題 2.7、問題 2.8、問題 2.9
コード
sample.scm
(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)) (define (upper-bound x) (cdr x)) (define (lower-bound x) (car x)) (define (sub-interval x y) (make-interval (- (lower-bound x) (upper-bound y)) (- (upper-bound x) (lower-bound y)))) (define (width-interval x) (/ (- (upper-bound x) (lower-bound x)) 2)) (define a (make-interval 10 100)) (define b (make-interval -50 50))
置き換えモデルで2つの区間(区間(a, b)、区間(c, d)の和、差から作った区間の幅を置き換えモデルでみてみる。
和
(width-interval (add-interval x y)) (width-interval (make-interval (+ (lower-bound x) (lower-bound y)) (+ (upper-bound x) (upper-bound y)))) (width-interval (cons (+ (lower-bound x) (lower-bound y)) (+ (upper-bound x) (upper-bound y)))) (/ (- (upper-bound (cons (+ (lower-bound x) (lower-bound y)) (+ (upper-bound x) (upper-bound y)))) (lower-bound (cons (+ (lower-bound x) (lower-bound y)) (+ (upper-bound x) (upper-bound y))))) 2) (/ (- (+ (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 x) (lower-bound y)) 2)) (+ (width-interval x) (width-interval y))
差
(width-interval (sub-interval x y)) (width-interval (make-interval (- (lower-bound x) (upper-bound y)) (- (upper-bound x) (lower-bound y)))) (width-interval (cons (- (lower-bound x) (upper-bound y)) (- (upper-bound x) (lower-bound y)))) (/ (- (- (upper-bound x) (lower-bound y)) (- (lower-bound x) (upper-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))
和、差の確認と乗算、除算では成り立たない例。
入出力結果(Terminal, REPL(Read, Eval, Print, Loop))
1 ]=> (define (width-add-sub-interval a b) (+ (width-interval a) (width-interval b))) ;Value: width-add-sub-interval 1 ]=> (width-add-sub-interval a b) ;Value: 95 1 ]=> (width-interval (add-interval a b)) ;Value: 95 1 ]=> (width-interval (sub-interval a b)) ;Value: 95 1 ]=> (width-interval (mul-interval a b)) ;Value: 5000 1 ]=> (width-interval (div-interval a b)) ;Value: 2.
乗算と除算では定義した区間の幅だけの関数(width-add-sub-interval)が成り立たない事が確認できた。
0 コメント:
コメントを投稿