From: Ramakrishnan Muthukrishnan Date: Thu, 6 Jan 2011 15:14:20 +0000 (+0530) Subject: works fine in guile. Not that well in racket for 100000 iterations. X-Git-Url: https://git.rkrishnan.org/about.html?a=commitdiff_plain;h=bb72ed35137c9d756e8df9c502b4538b4414c629;p=sicp.git works fine in guile. Not that well in racket for 100000 iterations. --- diff --git a/src/sicp/ex3_5.rkt b/src/sicp/ex3_5.rkt new file mode 100644 index 0000000..c93f777 --- /dev/null +++ b/src/sicp/ex3_5.rkt @@ -0,0 +1,35 @@ +#lang racket + +(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 (square x) + (* x x)) + +(define (random-in-range low high) + (let ((range (- high low))) + (+ low (random range)))) + +(define (P x y) + (define (inside-circle? radius centre-x centre-y) + (<= (+ (square (- x centre-x)) + (square (- y centre-y))) + (square radius))) + (inside-circle? 1.0 0 0)) + +(define (estimate-integral P x1 x2 y1 y2 trials) + (define (in-region) + (let ((x (random-in-range x1 x2)) + (y (random-in-range y1 y2))) + (P x y))) + (define (rectangle-area x1 x2 y1 y2) + (* (- x2 x1) (- y2 y1))) + + (* (monte-carlo trials in-region) + (rectangle-area x1 x2 y1 y2))) + \ No newline at end of file