2013年6月30日日曜日

開発環境

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

その他参考書籍

問題 3.5

コード(BBEdit)

sample.scm

(define (estimate-integral p x1 x2 y1 y2 trials)
  (define (experiment)
    (let ((x (random-in-range x2 x1))
          (y (random-in-range y2 y1)))
      (p x y)))
  (* (* (- x1 x2)
        (- y1 y2))
     (montecarlo trials experiment)))

;; 円周率πの見積もり(単位円の面積の見積もりと同じ)
(define unit-circle-area
  (estimate-integral (lambda (x y)
                       (let ((a (+ (square x)
                                   (square y))))
                         (or (< a 1) (= a 1))))
                     1.0
                     -1.0
                     1.0
                     -1.0
                     1000000))

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

1 ]=> unit-circle-area

;Value: 3.141316

0 コメント:

コメントを投稿