Chat (Lingr.com)
Informaiton
Daily
Column
- MySQL日本語の旅(5/1)
- アクセス向上秘伝(5/9)
- 一風変ったHaskellλ門(6/13)
- SICP Answer Book (5/31) 問題3.26追加
Zope Solution
Extra
アーカイブ
OSS案内所
Site Info
関連リンク
##(link2sicp "book-Z-H-12.html#%_thm_1.37" "Exercise 1.37")
解答例
a. 再帰プロセス版(再帰プロセス版 accumulate を使う)
(define (accumulate-rec combiner null-value term a next b)
(if (> a b)
null-value
(combiner (term a)
(accumulate-rec combiner null-value term (next a) next b))))
(define (cont-frac n d k)
(define (combiner x cf)
(/ (n x) (+ (d x) cf)))
(define (term i) i)
(define (next i) (+ i 1))
(accumulate-rec combiner (/ (n k) (d k)) term 1 next (- k 1)))
実行例
gosh> (cont-frac (lambda (i) 1.0) (lambda (i) 1.0) 9) 0.6181818181818182 gosh> (cont-frac (lambda (i) 1.0) (lambda (i) 1.0) 10) 0.6179775280898876 gosh> (cont-frac (lambda (i) 1.0) (lambda (i) 1.0) 11) 0.6180555555555556 gosh> (cont-frac (lambda (i) 1.0) (lambda (i) 1.0) 12) 0.6180257510729613
4桁の精度を得るためには、k を11以上にすればよい。
b. 反復プロセス版(反復プロセス版の accumulate を使う)
(define (accumulate-iter combiner null-value term a next b)
(if (> a b)
null-value
(accumulate-iter combiner (combiner null-value (term a)) term (next a) next b)))
(define (cont-frac n d k)
(define (combiner cf x)
(/ (n (+ (- k x) 1)) (+ (d (+ (- k x) 1)) cf)))
(define (term i) i)
(define (next i) (+ i 1))
(accumulate combiner (/ (n k) (d k)) term 1 next (- k 1)))
もうすこし読みやすい反復版(2005-03-18: thank you ....)
(define (cont-frac n d k)
(define (combiner cf x)
(/ (n x) (+ (d x) cf)))
(define (term i) (+ (- k i) 1))
(define (next i) (+ i 1))
(accumulate-iter combiner (/ (n k) (d k)) term 1 next k))
コード
##(sicp-answer-code "ex-1.37.scm")