Excercie 3.65

Excercie 3.65

Exercise 3.65.

まずは pi-stream を参考にして ln2-stream を作成。

(define (ln2-summands n)
  (stream-cons (/ 1.0 n)
               (stream-map - (ln2-summands (+ n 1)))))

(define ln2-stream
  (partial-sums (ln2-summands 1)))

まんまです。 次にストリームを必要な数だけ表示する displa-stream:

(define (display-stream stream n)
  (let loop ((i 0)
             (s stream))
    (when (< i n)
      (format #t "~s\n" (stream-car s))
      (loop (+ i 1) (stream-cdr s)))))

を用意して、

(display-stream ln2-stream 10)
(display-stream (euler-transform ln2-stream) 10)
(display-stream (accelerated-sequence euler-transform ln2-stream) 10)

この 3 つの出力を比較してみる

ln2-stream euler-transform accelerated-sequence
1.0 0.7 1.0
0.5 0.6904761904761905 0.7
0.8333333333333333 0.6944444444444444 0.6932773109243697
0.5833333333333333 0.6924242424242424 0.6931488693329254
0.7833333333333332 0.6935897435897436 0.6931471960735491
0.6166666666666666 0.6928571428571428 0.6931471806635636
0.7595238095238095 0.6933473389355742 0.6931471805604039
0.6345238095238095 0.6930033416875522 0.6931471805599445
0.7456349206349207 0.6932539682539683 0.6931471805599427
0.6456349206349207 0.6930657506744464 0.6931471805599454

以上。