2013年8月23日金曜日

開発環境

計算機プログラムの構造と解釈(Gerald Jay Sussman(原著)、Julie Sussman(原著)、Harold Abelson(原著)、和田 英一(翻訳)、ピアソンエデュケーション、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の3(標準部品化力, オブジェクトおよび状態)、3.5(ストリーム)、3.5.5(関数的プログラムの部品化度とオブジェクトの部品化度)、問題 3.82を解いてみる。

その他参考書籍

問題 3.82

コード(BBEdit)

sample.scm

(define (estimate-integral P x1 x2 y1 y2)
  (define (experiment-stream)
    (let ((x (random-in-range x1 x2))
          (y (random-in-range y1 y2)))
      (cons-stream (P x y)
                   (experiment-stream))))
  (let ((rect-area (* (- x2 x1)
                      (- y2 y1))))
    (stream-map (lambda (x)
                  (* rect-area x))
                (monte-carlo experiment-stream 0 0))))

;; 円周率は単位円(半径1)の面積と等しい
(define estimate-pi
  (estimate-integral (lambda (x y)
                       (<= (+ (square x)
                              (square y))
                           1))
                     -1.0
                     -1.0
                     1.0
                     1.0))    

0 コメント:

コメントを投稿