Finish exercises for chapter 3
parent
b20c05d6df
commit
3f2a8d1503
|
@ -180,6 +180,43 @@
|
|||
(assert (take 10 rs)
|
||||
(take 10 rs))
|
||||
|
||||
(display "\nex-3.82\n")
|
||||
(display "\nex-3.82 - estimate-integral-stream\n")
|
||||
|
||||
(define (rand) (random 65536))
|
||||
|
||||
(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 radius-circle 5000.)
|
||||
|
||||
(define (estimate-integral-stream P x1 x2 y1 y2)
|
||||
; Probably this stream should be based on a random stream,
|
||||
; but it does not really matter for the exercise.
|
||||
(define (area-test-stream)
|
||||
(cons-stream
|
||||
(P (random-in-range x1 (inc x2))
|
||||
(random-in-range y1 (inc y2)))
|
||||
(area-test-stream)))
|
||||
(let ((area-rectangle (* (abs (- x2 x1)) (abs (- y2 y1)))))
|
||||
(stream-map (lambda (x) (* x area-rectangle))
|
||||
(monte-carlo (area-test-stream) 0 0))))
|
||||
|
||||
(define (p x y) (<= (+ (square (- x radius-circle))
|
||||
(square (- y radius-circle)))
|
||||
(square radius-circle)))
|
||||
|
||||
(let ((area-circle-stream
|
||||
(estimate-integral-stream p 0 (* 2 radius-circle)
|
||||
0 (* 2 radius-circle))))
|
||||
(let ((pi (/ (stream-ref area-circle-stream 10000)
|
||||
(square radius-circle))))
|
||||
(assert (< pi 3.4) #t)
|
||||
(assert (> pi 3.0) #t)))
|
||||
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
(load "util.scm")
|
||||
|
||||
(display "\nex-4.1\n")
|
||||
|
Loading…
Reference in New Issue