2014年8月27日水曜日

開発環境

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

その他参考書籍

問題 3.79.

コード(BBEdit, Emacs)

sample79.scm

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

(load "./stream.scm")

(define (solve-2nd f y0 dy0 dt)
  (define y (integral (delay dy) y0 dt))
  (define dy (integral (delay ddy) dy0 dt))
  (define ddy (stream-map f dy y))
  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 (lambda (dy y) y) 1 1 0.001) n)))
 (stream-enumerate-interval 900 1000))

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

$ ./sample79.scm
2.4584972757507497
2.4609557730265004
2.463416728799527
2.4658801455283266
2.468346025673855
2.4708143716995288
2.4732851860712284
2.4757584712572998
2.478234229728557
2.4807124639582856
2.483193176422244
2.485676369598666
2.4881620459682647
2.490650208014233
2.4931408582222474
2.4956339990804697
2.49812963307955
2.5006277627126297
2.503128390475342
2.5056315188658176
2.5081371503846834
2.510645287535068
2.513155932822603
2.5156690887554256
2.518184757844181
2.5207029426020253
2.5232236455446273
2.525746869190172
2.5282726160593625
2.530800888675422
2.5333316895640974
2.5358650212536613
2.538400886274915
2.5409392871611898
2.543480226448351
2.5460237066747995
2.5485697303814745
2.551118300111856
2.553669418411968
2.55622308783038
2.55877931091821
2.5613380902291283
2.5638994283193575
2.5664633277476767
2.5690297910754243
2.5715988208664995
2.574170419687366
2.576744590107053
2.5793213346971604
2.5819006560318574
2.584482556687889
2.587067039244577
2.5896541062838216
2.5922437603901054
2.5948360041504954
2.597430840154646
2.600028270994801
2.6026282992657954
2.6052309275650614
2.6078361584926264
2.610443994651119
2.61305443864577
2.6156674930844157
2.6182831605775
2.6209014437380778
2.623522345181816
2.6261458675269975
2.6287720133945247
2.6314007854079193
2.6340321861933274
2.6366662183795206
2.6393028845979
2.6419421874824978
2.64458412966998
2.64722871379965
2.6498759425134497
2.6525258184559632
2.655178344274419
2.6578335226186933
2.660491356141312
2.663151847497453
2.6658149993449505
2.6684808143442953
2.6711492951586395
2.6738204444537983
2.676494264898252
2.6791707591631506
2.681849929922314
2.6845317798522363
2.6872163116320884
2.6899035279437205
2.692593431471664
2.695286024903136
2.697981310928039
2.700679292238967
2.7033799715312057
2.7060833515027367
2.7087894348542396
2.7114982242890937
2.7142097225133828
2.716923932235896
$

0 コメント:

コメントを投稿