2014年8月26日火曜日

開発環境

計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の3(標準部品化力、オブジェクトおよび状態)、3.5(ストリーム)、3.5.4(ストリームと遅延評価)、問題 3.78.を解いてみる。

その他参考書籍

問題 3.78.

コード(BBEdit, Emacs)

sample78.scm

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

(load "./stream.scm")

(define (solve-2nd a b dt y0 dy0)
  (define y (integral (delay dy) y0 dt))
  (define dy (integral (delay ddy) dy0 dt))
  (define ddy (add-streams (scale-stream dy a)
                           (scale-stream y b)))
  y)

(define (integral delayed-integrand initial-value dt)
  (cons-stream initial-value
               (let ((integrand (force delayed-integrand)))
                 (if (stream-null? integrand)
                     the-empty-stream
                     (integral (delay (stream-cdr integrand))
                               (+ (* dt (stream-car integrand))
                                  initial-value)
                               dt)))))

(stream-for-each
 (lambda (n)
   (print (stream-ref (solve-2nd 1 2 0.001 3 4) n)))
 (stream-enumerate-interval 900 1000))

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

$ ./sample78.scm
14.361416599758837
14.389326659713264
14.417293352560845
14.445316790754594
14.473397086973241
14.501534354121688
14.529728705331458
14.557980253961146
14.586289113596875
14.614655398052747
14.643079221371302
14.67156069782397
14.700099941911535
14.728697068364584
14.75735219214397
14.78606542844127
14.814836892679253
14.84366670051233
14.872554967827025
14.901501810742436
14.9305073456107
14.959571689017451
14.988694957782302
15.017877268959296
15.047118739837382
15.076419487940884
15.105779631029968
15.135199287101118
15.164678574387601
15.194217611359944
15.223816516726409
15.253475409433463
15.283194408666258
15.312973633849104
15.342813204645951
15.372713240960863
15.402673862938498
15.432695190964592
15.462777345666439
15.492920447913368
15.523124618817237
15.553389979732906
15.583716652258728
15.614104758237035
15.644554419754625
15.67506575914325
15.705638898980101
15.736273962088308
15.766971071537421
15.797730350643908
15.828551922971645
15.85943591233241
15.890382442786382
15.921391638642634
15.952463624459627
15.983598525045714
16.014796465459636
16.046057571011023
16.07738196726089
16.10876978002215
16.140221135360107
16.17173615959296
16.20331497929232
16.234957721283696
16.266664512647022
16.298435480717153
16.33027075308438
16.36217045759494
16.394134722351513
16.426163675713756
16.458257446298806
16.490416162981795
16.52263995489636
16.554928951435162
16.587283282250414
16.619703077254385
16.652188466619926
16.684739580780985
16.717356550433138
16.750039506534105
16.782788580304274
16.815603903227228
16.848485607050264
16.88143382378493
16.914448685707544
16.94753032535973
16.980678875548936
17.013894469348983
17.04717724010058
17.08052732141187
17.11394484715895
17.147429951486423
17.180982768807915
17.214603433806634
17.24829208143589
17.28204884691964
17.315873865753037
17.349767273702962
17.383729206808567
17.417759801381827
17.451859194008073
$

0 コメント:

コメントを投稿