6 (define (withdraw amount)
7 (if (>= (- balance amount) 0)
9 (set! balance (- balance amount))
11 (print "insufficient balance")))
16 (if (>= (- balance amount) 0)
18 (set! balance (- balance amount))
20 "insufficient funds"))))
22 (define (make-withdraw balance)
24 (if (>= (- balance amount) 0)
26 (set! balance (- balance amount))
28 "insufficent funds")))
30 (define w1 (make-withdraw 100))
31 (define w2 (make-withdraw 200))
33 (define (make-account balance)
34 (define (withdraw amount)
35 (if (>= (- balance amount) 0)
37 (set! balance (- balance amount))
39 "insufficient funds"))
40 (define (deposit amount)
42 (set! balance (+ balance amount))
46 ((eq? m 'withdraw) (lambda (amount) (withdraw amount)))
47 ((eq? m 'deposit) (lambda (amount) (deposit amount)))
48 (else (error "unknown request -- make-account " m))))
57 (set! x (rand-update x))
60 ;; monte carlo simulation
62 (define (estimate-pi trials)
63 (sqrt (/ 6 (monte-carlo trials cesaro-test))))
66 (= (gcd (rand) (rand)) 1))
68 (define (monte-carlo trials experiment)
69 (define (iter trials-remaining trials-passed)
71 ((= trials-remaining 0) (/ trials-passed trials))
72 ((experiment) (iter (- trials-remaining 1) (+ trials-passed 1)))
73 (else (iter (- trials-remaining 1) trials-passed))))
77 (define (estimate-pi2 trials)
78 (sqrt (/ 6 (random-gcd-trials trials random-init))))
80 (define (random-gcd-trials trials initial-x)
81 (define (iter trials-remaining trials-passed x)
82 (let ((x1 (rand-update x)))
83 (let ((x2 (rand-update x1)))
85 ((= trials-ramaining 0) (/ trials-passed trials))
86 ((= (gcd x1 x2) 1) (iter (- trials-remaining 1)
89 (else (iter (- trials-remaining 1)
92 (iter trials 0 initial-x))