6th Thu
素人くさいSICP勉強会#20レポート
This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/62
素人くさいSICP読書会に参加してきました。
ここのところ数回続けて参加できていなかったので、参加できて良かったです。
参加者数は、だいたい15名前後でした。今までと同様、演習問題を黙々と解いていくという形式で進められました。今回の範囲は、演習問題 2.9〜2.11でした。 間が空いていたので、前回までに進んだところを追いかけながら進めました。
途中、IntervalのWidthが減る事があるかどうか、という議論がありましたが、Widthは誤差のようなものなので、演算の結果として減る事は無いですね。
ということで、今回使ったコードを置いておきます(ちゃんと検証してないから書き間違いがあるかも)
;;7/5
(define (make-interval a b) (cons a b))
(define (lower-bound x) (car x))
(define (upper-bound x) (cdr x))
(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)))))
;; ex 2.8
(define (sub-interval x y)
(make-interval (- (lower-bound x) (upper-bound y))
(- (upper-bound x) (lower-bound y))))
;; ex 2.10
(define (div-interval-2 x y)
(if (and (>= (upper-bound y) 0) (<= (lower-bound y) 0))
(display "error: divider is spanning zero.")
(div-interval x y)))
;; ex 2.11
(define (mul-interval-2 x y)
(define (pp-pp-mul s t)
(make-interval (* (lower-bound s) (lower-bound t))
(* (upper-bound s) (upper-bound t))))
(define (pp-mm-mul s t)
(make-interval (* (upper-bound s) (lower-bound t))
(* (lower-bound s) (upper-bound t))))
(define (pp-pm-mul s t)
(make-interval (* (upper-bound s) (lower-bound t))
(* (upper-bound s) (upper-bound t))))
(define (mm-mm-mul s t)
(make-interval (* (upper-bound s) (upper-bound t))
(* (lower-bound s) (lower-bound t))))
(define (mm-pm-mul s t)
(make-interval (* (lower-bound s) (upper-bound t))
(* (lower-bound s) (lower-bound t))))
(define (mm-pp-mul s t) (pp-mm-mul t s))
(define (pm-pp-mul s t) (pp-pm-mul t s))
(define (pm-mm-mul s t) (mm-pm-mul t s))
(define (pm-pm-mul s t)
(make-interval (min (* (upper-bound s) (lower-bound t))
(* (lower-bound s) (upper-bound t)))
(max (* (lower-bound s) (lower-bound t))
(* (upper-bound s) (upper-bound t)))))
(define (pp s) (> (lower-bound s) 0))
(define (mm s) (< (upper-bound s) 0))
(define (pm s) (not (or (pp s) (mm s))))
(cond ((and (pp x) (pp y)) (pp-pp-mul x y))
((and (pp x) (mm y)) (pp-mm-mul x y))
((and (pp x) (pm y)) (pp-pm-mul x y))
((and (mm x) (pp y)) (mm-pp-mul x y))
((and (mm x) (mm y)) (mm-mm-mul x y))
((and (mm x) (pm y)) (mm-pm-mul x y))
((and (pm x) (pp y)) (pm-pp-mul x y))
((and (pm x) (mm y)) (pm-mm-mul x y))
((and (pm x) (pm y)) (pm-pm-mul x y))))
This article was migrated from http://rails.office.drecom.jp/takiuchi/archive/62
posted by
genki on Thu 6 Jul 2006 at 16:12 with 0 comments