計算機プログラムの構造と解釈[第2版]
(翔泳社)
ハロルド エイブルソン (著) ジュリー サスマン (著)
ジェラルド・ジェイ サスマン (著)
Harold Abelson (原著) Julie Sussman (原著)
Gerald Jay Sussman (原著) 和田 英一 (翻訳)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Scheme (プログラミング言語)
- Gauche (処理系)
計算機プログラムの構造と解釈[第2版](ハロルド エイブルソン (著)、ジュリー サスマン (著)、ジェラルド・ジェイ サスマン (著)、Harold Abelson (原著)、Julie Sussman (原著)、Gerald Jay Sussman (原著)、和田 英一 (翻訳)、翔泳社、原書: Structure and Interpretation of Computer Programs (MIT Electrical Engineering and Computer Science)(SICP))の3(標準部品化力、オブジェクトおよび状態)、3.1(代入と局所状態)、3.1.2(代入を取り入れた利点)、問題 3.5.を解いてみる。
その他参考書籍
- Instructor's Manual to Accompany Structure & Interpretation of Computer Programs
- プログラミングGauche (Kahuaプロジェクト (著), 川合 史朗 (監修), オライリージャパン)
問題 3.5.
コード(BBEdit, Emacs)
sample3_3.scm
#!/usr/bin/env gosh
;; -*- coding: utf-8 -*-
;; random-integerを使う
(use srfi-27)
(define (monte-carlo trials experiment)
(define (iter trials-remaining trials-passed)
(cond ((= trials-remaining 0)
(/ trials-passed trials))
((experiment)
(iter (- trials-remaining 1)
(+ trials-passed 1)))
(else
(iter (- trials-remaining 1)
trials-passed))))
(iter trials 0))
(define (random-in-range low high)
(let ((range (- high low)))
(+ low (random-integer range))))
(define (estimate-integral pred x1 x2 y1 y2 trials)
(* (* (- x2 x1)
(- y2 y1))
(monte-carlo trials
(lambda ()
(pred (random-in-range x1 x2)
(random-in-range y1 y2))))))
(define (estimate-pi trials)
(estimate-integral (lambda (x y) (<= (+ (* x x)
(* y y))
1))
-1 1 -1 1
trials))
;; テスト
(for-each (lambda (trials)
(print (estimate-pi trials)))
(list 100.0 1000.0 10000.0 100000.0 1000000.0))
入出力結果(Terminal(gosh), REPL(Read, Eval, Print, Loop))
$ ./sample3_5.scm 3.0 2.94 3.038 3.00088 3.0012 $
0 コメント:
コメントを投稿