2014年8月28日木曜日

開発環境

計算機プログラムの構造と解釈[第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.80.を解いてみる。

その他参考書籍

問題 3.80.

コード(BBEdit, Emacs)

sample80.scm

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

(load "./stream.scm")

(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)))))

(define (RLC R L C dt)
  (define (inner vc0 il0)
    (define vc (integral (delay dvc) vc0 dt))
    (define il (integral (delay dil) il0 dt))
    (define dvc (scale-stream il (/ -1 C)))
    (define dil (add-streams (scale-stream vc (/ 1 L))
                             (scale-stream il (* -1 (/ R L)))))
    (stream-map (lambda (v i) (cons v i)) vc il))
  inner)

(define RLC1 (RLC 1 0.2 1 0.1))

(stream-for-each
 (lambda (n)
   (print (stream-ref (RLC1 10 0) n)))
 (stream-enumerate-interval 0 100))

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

$ ./sample80.scm
(10 . 0)
(10.0 . 5.0)
(9.5 . 7.5)
(8.75 . 8.5)
(7.9 . 8.625)
(7.0375000000000005 . 8.2625)
(6.211250000000001 . 7.6499999999999995)
(5.446250000000001 . 6.930624999999999)
(4.753187500000001 . 6.1884375)
(4.134343750000001 . 5.470812500000001)
(3.587262500000001 . 4.802578125000001)
(3.107004687500001 . 4.194920312500001)
(2.687512656250001 . 3.6509625000000008)
(2.322416406250001 . 3.1692375781250006)
(2.005492648437501 . 2.7458269921875007)
(1.7309099492187507 . 2.375659820312501)
(1.4933439671875006 . 2.0532848847656258)
(1.2880154787109381 . 1.7733144259765632)
(1.1106840361132817 . 1.5306649523437508)
(0.9576175408789066 . 1.3206744942285162)
(0.825550091456055 . 1.1391460175537114)
(0.7116354897006838 . 0.9823480545048832)
(0.6134006842501954 . 0.8469917721027835)
(0.528701507039917 . 0.7301962281764895)
(0.4556818842222681 . 0.6294488676082033)
(0.39273699746144775 . 0.5425653759152357)
(0.33848045986992414 . 0.4676511866883417)
(0.29171534120109 . 0.40306582327913293)
(0.2514087588731767 . 0.34739058224011143)
(0.21666970064916558 . 0.29939967055664407)
(0.18672973359350117 . 0.2580346856029048)
(0.1609262650332107 . 0.222382209598203)
(0.1386880440733904 . 0.19165423731570685)
(0.11952262034181971 . 0.16517114069454863)
(0.10300550627236485 . 0.14234688051818417)
(0.08877081822054643 . 0.1226761933952745)
(0.07650319888101897 . 0.10572350580791047)
(0.06593084830022793 . 0.09111335234446472)
(0.05681951306578145 . 0.07852210032234633)
(0.04896730303354682 . 0.06767080669406389)
(0.042200222364140436 . 0.058319054863805356)
(0.0363683168777599 . 0.050259638613972896)
(0.03134235301636261 . 0.0433139777458664)
(0.02701095524177597 . 0.037328165381114504)
(0.02327813870366452 . 0.03216956031144524)
(0.020061182672519995 . 0.02772384950755488)
(0.017288797721764507 . 0.02389251609003744)
(0.014899546112760764 . 0.02059065690590097)
(0.012840480422170666 . 0.01774510150933087)
(0.011065970271237579 . 0.015292790965750768)
(0.009536691174662501 . 0.013179380618494173)
(0.008218753112813084 . 0.011358035896578337)
(0.00708294952315525 . 0.009788394504695711)
(0.006104110072685679 . 0.00843567201392548)
(0.0052605428712931305 . 0.00726989104330558)
(0.0045335537669625725 . 0.006265216957299355)
(0.003907032071232637 . 0.005399385362130964)
(0.003367093535019541 . 0.0046532087166818005)
(0.002901772663351361 . 0.004010151125850671)
(0.002500757550766294 . 0.003455961894601016)
(0.002155161361306192 . 0.002978359722683655)
(0.0018573253890378266 . 0.0025667605419949233)
(0.0016006493348383344 . 0.002212042965516375)
(0.0013794450382866967 . 0.0019063461501773547)
(0.0011888104232689613 . 0.0016428955942320257)
(0.0010245208638457587 . 0.0014158530087504935)
(8.829355629707094e-4 . 0.0012201869362981261)
(7.609168693408967e-4 . 0.0010515612496344179)
(6.557607443774549e-4 . 9.062390594876573e-4)
(5.651368384286892e-4 . 7.809999019325561e-4)
(4.8703684823543354e-4 . 6.730683701806227e-4)
(4.1973001121737126e-4 . 5.800526092080281e-4)
(3.617247502965685e-4 . 4.998913102126996e-4)
(3.117356192752985e-4 . 4.3080803025463406e-4)
(2.686548162498351e-4 . 3.712718247649663e-4)
(2.3152763377333845e-4 . 3.199633205074007e-4)
(1.9953130172259837e-4 . 2.7574547714036957e-4)
(1.7195675400856142e-4 . 2.37638389431484e-4)
(1.4819291506541302e-4 . 2.047975717200227e-4)
(1.2771315789341075e-4 . 1.7649524339271786e-4)
(1.1006363355413897e-4 . 1.521042006430643e-4)
(9.485321348983254e-5 . 1.3108391709860164e-4)
(8.174482177997237e-5 . 1.1296856529421708e-4)
(7.044796525055066e-5 . 9.735669353709472e-5)
(6.0712295896841186e-5 . 8.39023293938227e-5)
(5.232206295745891e-5 . 7.230731264533193e-5)
(4.509133169292572e-5 . 6.231468780139543e-5)
(3.885986291278618e-5 . 5.3703009747160575e-5)
(3.348956193807012e-5 . 4.628143632997337e-5)
(2.8861418305072784e-5 . 3.988549913402175e-5)
(2.487286839167061e-5 . 3.437345871954727e-5)
(2.143552251971588e-5 . 2.9623163555608938e-5)
(1.8473206164154986e-5 . 2.552934303766241e-5)
(1.5920271860388744e-5 . 2.2001274600908698e-5)
(1.3720144400297874e-5 . 1.896077323064872e-5)
(1.1824067077233002e-5 . 1.6340458815473298e-5)
(1.0190021195685671e-5 . 1.408226294635315e-5)
(8.781794901050356e-6 . 1.213614207101941e-5)
(7.568180693948414e-6 . 1.0458968486034883e-5)
(6.522283845344926e-6 . 9.013574589991648e-6)
(5.620926386345761e-6 . 7.767929217668287e-6)
$

0 コメント:

コメントを投稿