diff options
| author | Mike Vink <mike1994vink@gmail.com> | 2023-05-13 21:12:33 +0200 |
|---|---|---|
| committer | Mike Vink <mike1994vink@gmail.com> | 2023-05-13 21:12:33 +0200 |
| commit | 89e254e7e92688dac934e4f9404c4dfa69656943 (patch) | |
| tree | 010a1d29863d660764d11a9242cb626407402fc1 /coding-exercises/3 | |
| parent | 47d68a308febd22174aae26c82fa3b1b5088a866 (diff) | |
3.10
Diffstat (limited to 'coding-exercises/3')
| -rw-r--r-- | coding-exercises/3/5.rkt | 41 | ||||
| -rw-r--r-- | coding-exercises/3/7.rkt | 73 |
2 files changed, 114 insertions, 0 deletions
diff --git a/coding-exercises/3/5.rkt b/coding-exercises/3/5.rkt index e69de29..438db99 100644 --- a/coding-exercises/3/5.rkt +++ b/coding-exercises/3/5.rkt @@ -0,0 +1,41 @@ +#lang racket +;; 5 +(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 ((r (- high low))) + (+ low (* r (random))))) + +(define (estimate-integral P x1 x2 y1 y2 n) + (* (monte-carlo n (lambda () (P (random-in-range x1 x2) (random-in-range y1 y2)))) + (* (- x2 x1) + (- y2 y1)))) + +(estimate-integral + (lambda (x y) (<= (+ (sqr (- x 5)) (sqr (- y 7))) 1)) + 4 6 + 6 8 + 100) + +;; 6 +(define (make-rand seed) + (define (rand-update) + (random)) + (define (rand-reset reset) + (random-seed reset)) + (define (dispatch m . args) + (cond ((eq? m 'generate) (rand-update)) + ((eq? m 'reset) (rand-reset (car args))))) + dispatch) +(define rand (make-rand 43)) +(rand 'generate) +(rand 'reset 42) diff --git a/coding-exercises/3/7.rkt b/coding-exercises/3/7.rkt new file mode 100644 index 0000000..b104ad1 --- /dev/null +++ b/coding-exercises/3/7.rkt @@ -0,0 +1,73 @@ +#lang racket +;; 7 +;; import from 1.rkt +(define (make-accumulator amount) + (lambda (add-this) + (set! amount (+ amount add-this)) + amount)) + +(define (make-monitored fn) + (define calls (make-accumulator 0)) + (lambda (arg-or-message) + (cond ((equal? arg-or-message 'how-many-calls?) (calls 0)) + ((equal? arg-or-message 'reset-count) (set! calls (make-accumulator 0))) + (else (begin + (calls 1) + (fn arg-or-message)))))) +(define s (make-monitored sqrt)) + +(define (make-account balance secret) + (define secrets (list secret)) + + (define (withdraw amount) + (if (>= balance amount) + (begin (set! balance (- balance amount)) balance) + "Insufficient funds")) + (define (deposit amount) + (set! balance (+ balance amount)) + balance) + (define (new-password password) + (set! secrets (cons password secrets))) + (define (dispatch m) + (cond ((eq? m 'withdraw) withdraw) + ((eq? m 'deposit) deposit) + ((eq? m 'new-password) new-password) + (else (error "Unknown request -- MAKE-ACCOUNT" m)))) + + (define (args->dispatch fn) + (lambda (arg-list) + (let ((password (car arg-list)) + (m (cadr arg-list))) + (if (memq password secrets) + (fn m) + (lambda (a . n) + "Wrong password"))))) + + (define monitored-dispatch (make-monitored (args->dispatch dispatch))) + (define (call-the-cops) + (lambda (a . n) + "Calling the cops")) + + (define (safe-dispatch . args) + (if (> 7 (monitored-dispatch 'how-many-calls?)) + (monitored-dispatch args) + (call-the-cops))) + safe-dispatch) + +(define (make-joint account password new-password) + ((account password 'new-password) new-password)) + +(define paul-acc (make-account 100 'open-sesame)) +((paul-acc 'open-sesame 'withdraw) 10) +(make-joint paul-acc 'open-sesame 'rosebud) +((paul-acc 'rosebud 'withdraw) 10) + +;; 8 +(define hidden '()) +(define (f v) + (if (null? hidden) + (begin + (set! hidden v) + hidden) + 0)) +(+ (f 0) (f 0)) |
