2014年8月30日土曜日

開発環境

計算機プログラムの構造と解釈[第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.5(関数的プログラムの部品化度とオブジェクトの部品化度)、問題 3.82.を解いてみる。

その他参考書籍

問題 3.82.

コード(BBEdit, Emacs)

sample81.scm

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

;; random-integerを使う
(use srfi-27)

(load "./stream.scm")

(define (random-in-range low high)
  (let ((range (- high low)))
    (+ low (* (random-real) range))))

(define (map-successive-pairs f s)
  (cons-stream
   (f (stream-car s) (stream-car (stream-cdr s)))
   (map-successive-pairs f (stream-cdr (stream-cdr s)))))

(define (monte-carlo experiment-stream passed failed)
  (define (next passed failed)
    (cons-stream
     (/ passed (+ passed failed))
     (monte-carlo
      (stream-cdr experiment-stream) passed failed)))
  (if (stream-car experiment-stream)
      (next (+ passed 1) failed)
      (next passed ( + failed 1))))

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

(define area
  (estimate-integral (lambda (x y)
                       (<= (+ (expt (- x 5) 2)
                              (expt (- y 7) 2))
                           (expt 3 2)))
                     2 8 4 10))

(print "円の面積")
(print "モンテカルロ積分")
(print "0から100")
(stream-for-each (lambda (n) (print (* 1.0 (stream-ref area n))))
                 (stream-enumerate-interval 0 100))

(print "100000から100010")
(stream-for-each (lambda (n) (print (* 1.0 (stream-ref area n))))
                 (stream-enumerate-interval 100000 100010))

(print "円周率を3.14として、公式で計算")
(print (* (expt 3 2) 3.14))


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

$ ./sample82.scm
円の面積
モンテカルロ積分
0から100
0.0
0.0
12.0
18.0
14.4
12.0
15.428571428571429
18.0
20.0
18.0
19.636363636363637
18.0
19.384615384615383
20.571428571428573
21.6
20.25
19.058823529411764
18.0
18.94736842105263
18.0
18.857142857142858
19.636363636363637
20.347826086956523
21.0
21.6
22.153846153846153
22.666666666666668
23.142857142857142
23.586206896551722
24.0
24.387096774193548
24.75
24.0
24.352941176470587
24.685714285714287
25.0
25.2972972972973
25.57894736842105
25.846153846153847
26.1
26.341463414634145
26.571428571428573
26.790697674418606
27.0
27.2
26.608695652173914
26.80851063829787
26.25
26.448979591836736
25.92
26.11764705882353
26.307692307692307
26.49056603773585
26.666666666666668
26.836363636363636
27.0
27.157894736842106
27.310344827586206
27.45762711864407
27.6
27.147540983606557
26.70967741935484
26.857142857142858
27.0
27.138461538461538
27.272727272727273
27.402985074626866
27.0
27.130434782608695
27.257142857142856
27.380281690140844
27.5
27.616438356164384
27.72972972972973
27.84
27.94736842105263
27.584415584415584
27.692307692307693
27.79746835443038
27.9
28.0
28.097560975609756
27.759036144578314
27.428571428571427
27.529411764705884
27.627906976744185
27.310344827586206
27.0
27.10112359550562
27.2
27.296703296703296
27.391304347826086
27.483870967741936
27.574468085106382
27.28421052631579
27.0
27.09278350515464
27.183673469387756
27.272727272727273
27.36
27.445544554455445
100000から100010
28.25719742802572
28.25727485450291
28.25735227943162
28.257429702811887
28.25750712464377
28.257584544927305
28.257661963662542
28.257739380849532
28.257816796488317
28.257894210578943
28.257971623121456
円周率を3.14として、公式で計算
28.26
$

0 コメント:

コメントを投稿