(define balance 100)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define new-withdraw
(let ((balance 100))
(lambda (amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))))
(define (make-withdraw balance)
(lambda (amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds")))
(define W1 (make-withdraw 100))
(define W2 (make-withdraw 100))
(define (make-account balance)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposiot amount)
(set! balance (+ balance amount))
balance)
(define (dispatch m)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposiot)
(else (error "Unknown request -- MAKE-ACCOUNT"
m))))
dispatch)
(define (make-accumulator balance)
(lambda (amount)
(begin (set! balance (+ balance amount))
balance)))
(define A (make-accumulator 5))
(define B (make-accumulator 5))
(define (make-monitored proc)
(let ((counter 0))
(lambda (x)
(cond ((eq? x 'how-many-calls?) counter)
((eq? x 'reset-count) (set! counter 0))
(else
(begin (set! counter (+ counter 1))
(proc x)))))))
(define (make-monitored proc)
(let ((counter 0))
(define (do-proc x) (begin (set! counter (+ counter 1))
(proc x)))
(define (return-counter x) counter)
(define (reset-counter x) (set! counter 0))
(define (dispatch m)
(cond ((eq? m 'how-many-calls?) (return-counter m))
((eq? m 'reset-count) (reset-counter m))
(else (do-proc m))))
dispatch))
(define s (make-monitored sqrt))
(define (make-account balance my-password)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposiot amount)
(set! balance (+ balance amount))
balance)
(define (dispatch password m)
(if (eq? my-password password)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposiot)
(else (error "Unknown request -- MAKE-ACCOUNT" m)))
(error "Incorrect password")))
dispatch)
(define (make-account balance my-password)
(let ((access-counter 0))
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposiot amount)
(set! balance (+ balance amount))
balance)
(define (call-the-cops)
(error "call-the-cops"))
(define (dispatch password m)
(if (>= access-counter 7)
(call-the-cops)
(if (eq? my-password password)
(begin (set! access-counter 0)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposiot)
(else (error "Unknown request -- MAKE-ACCOUNT" m))))
(begin (set! access-counter (+ access-counter 1))
(print "access-counter " access-counter)
(error "Incorrect password")))))
dispatch))
(define random-init 12345)
(define (rand-update x)
(modulo (+ (* 214013 x) 253011) 32767))
(define rand
(let ((x random-init))
(lambda ()
(set! x (rand-update x))
x)))
(define (estimate-pi trials)
(sqrt (/ 6 (monte-carlo trials cesaro-test))))
(define (cesaro-test)
(= (gcd (rand) (rand)) 1))
(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))
(use srfi-27)
(define (random-in-range low high)
(let ((range (- high low)))
(+ low (random-integer range))))
(define (estimate-integral p x1 x2 y1 y2 trials)
(*
(monte-carlo trials (lambda () (p (random-in-range x1 x2) (random-in-range y1 y2))))
(* (- x2 x1) (- y2 y1))))
(define (pi-from-monte-carlo-simulation circle-area radius)
(/ circle-area radius))
(define (p-test x y)
(<= (+ (square (- x 5)) (square (- y 7))) (square 3)))
(define rand
(let ((x random-init))
(define generate
(lambda ()
(set! x (rand-update x)) x))
(define (reset new-value)
(begin (set! x new-value) x))
(define (dispatch m)
(cond ((eq? m 'generate) (generate))
((eq? m 'reset) reset)
(else (error "Unknown requeset -- RAND" m))))
dispatch))
(define (make-account balance my-password)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposiot amount)
(set! balance (+ balance amount))
balance)
(define (dispatch password m)
(if (eq? my-password password)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposiot)
((eq? m 'password?) #t)
(else (error "Unknown request -- MAKE-ACCOUNT" m)))
(error "Incorrect password")))
dispatch)
(define (make-joint account account-password new-password)
(define (dispatch password m)
(if (and (account account-password 'password?) (eq? new-password password))
(account account-password m)
(error "Incorrect joint password")))
dispatch)
(define peter-acc (make-account 100 'open-sesame))
(define paul-acc
(make-joint peter-acc 'open-sesame 'rosebud))