Commit e05f0885 authored by bbguimaraes's avatar bbguimaraes
Browse files

sicp: section 3.1

parent 728ba081
(define balance 100)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(display (withdraw 25))
(newline)
(display (withdraw 25))
(newline)
(display (withdraw 60))
(newline)
(display (withdraw 15))
(newline)
(define new-withdraw
(let ((balance 100))
(lambda (amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))))
(newline)
(display (new-withdraw 25))
(newline)
(display (new-withdraw 25))
(newline)
(display (new-withdraw 60))
(newline)
(display (new-withdraw 15))
(newline)
(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))
(newline)
(display (W1 50))
(newline)
(display (W2 70))
(newline)
(display (W2 40))
(newline)
(display (W1 40))
(newline)
(define (make-account balance)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (dispatch m)
(cond ((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT"
m))))
dispatch)
(define acc (make-account 100))
(newline)
(display ((acc 'withdraw) 50))
(newline)
(display ((acc 'withdraw) 60))
(newline)
(display ((acc 'deposit) 40))
(newline)
(display ((acc 'withdraw) 60))
(newline)
(define acc2 (make-account 100))
(newline)
(display ((acc2 'withdraw) 50))
(newline)
(define one-arbitrary-number 9999999)
(define another-arbitrary-number 200000)
(define random-init (random one-arbitrary-number))
(define (rand-update x) (random one-arbitrary-number))
(define rand
(let ((x random-init))
(lambda ()
(set! x (rand-update x))
x)))
(display (rand))
(newline)
(display (rand))
(newline)
(display (rand))
(newline)
(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))
(display (estimate-pi another-arbitrary-number))
(newline)
(define (estimate-pi trials)
(sqrt (/ 7 (random-gcd-test trials random-init))))
(define (random-gcd-test trials initial-x)
(define (iter trials-remaining trials-passed x)
(let ((x1 (rand-update x)))
(let ((x2 (rand-update x1)))
(cond ((= trials-remaining 0)
(/ trials-passed trials))
((= (gcd x1 x2) 1)
(iter (- trials-remaining 1)
(+ trials-passed 1)
x2))
(else
(iter (- trials-remaining 1)
trials-passed
x2))))))
(iter trials 0 initial-x))
(display (estimate-pi another-arbitrary-number))
(newline)
(define (make-simplified-withdraw balance)
(lambda (amount)
(set! balance (- balance amount))
balance))
(define W (make-simplified-withdraw 25))
(map display (list (W 20) " " (W 10) "\n"))
(define (make-decrementer balance)
(lambda (amount)
(- balance amount)))
(define D (make-decrementer 25))
(map display (list (D 20) " " (D 10) "\n"))
(define D1 (make-decrementer 25))
(define D2 (make-decrementer 25))
(define W1 (make-simplified-withdraw 25))
(define W2 (make-simplified-withdraw 25))
(map display (list (D1 20) " " (D1 20) " " (D2 20) "\n"))
(map display (list (W1 20) " " (W1 20) " " (W2 20) "\n"))
(define (factorial n)
(let ((product 1)
(counter 1))
(define (iter)
(if (> counter n)
product
(begin (set! product (* counter product))
(set! counter (+ counter 1))
(iter))))
(iter)))
(display (factorial 5))
(newline)
(define (make-accumulator n)
(lambda (x)
(set! n (+ n x))
n))
(define A (make-accumulator 5))
(display (A 10))
(newline)
(display (A 10))
(newline)
(define (make-monitored f)
(define call-count 0)
(define (mf x)
(cond ((eq? x 'how-many-calls?) call-count)
((eq? x 'reset-count) (set! call-count 0))
(else
(set! call-count (+ 1 call-count))
(f x))))
mf)
(define s (make-monitored sqrt))
(display (s 100))
(newline)
(display (s 'how-many-calls?))
(newline)
(display (s 25))
(newline)
(display (s 'how-many-calls?))
(newline)
(display (s 'reset-count))
(newline)
(display (s 'how-many-calls?))
(newline)
(define (make-account balance password)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (wrong-password . args) "Incorrect password")
(define (dispatch p m)
(cond ((not (eq? p password)) wrong-password)
((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT"
m))))
dispatch)
(define acc (make-account 100 'secret-password))
(display ((acc 'secret-password 'withdraw) 40))
(newline)
(display ((acc 'some-other-password 'deposit) 50))
(newline)
(define (make-account balance password)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define wrong-password-count 0)
(define (wrong-password . args)
(set! wrong-password-count (+ 1 wrong-password-count))
(if (> wrong-password-count 7) (call-the-cops))
"Incorrect password")
(define (dispatch p m)
(cond ((not (eq? p password)) wrong-password)
((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT"
m))))
dispatch)
(define (call-the-cops) (display "the cops have been called\n"))
(define acc (make-account 100 'secret-password))
(display ((acc 'secret-password 'withdraw) 40))
(newline)
(display ((acc '0 'deposit) 50))
(newline)
(display ((acc '1 'deposit) 50))
(newline)
(display ((acc '2 'deposit) 50))
(newline)
(display ((acc '3 'deposit) 50))
(newline)
(display ((acc '4 'deposit) 50))
(newline)
(display ((acc '5 'deposit) 50))
(newline)
(display ((acc '6 'deposit) 50))
(newline)
(display ((acc '7 'deposit) 50))
(newline)
(define (estimate-integral P x1 x2 y1 y2 trials)
(define (experiment)
(define (random-in-range low high)
(let ((range (- high low)))
(+ low (random range))))
(P (random-in-range x1 x2) (random-in-range y1 y2)))
(* (* (- x2 x1) (- y2 y1))
(monte-carlo trials experiment)))
(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 one-arbitrary-number 33333)
(define (inside-circle-predicate xc yc r)
(let ((r2 (square r)))
(lambda (x y) (<= (+ (square (- x xc)) (square (- y yc))) r2))))
(display
(estimate-integral
(inside-circle-predicate 5 7 3)
2.0 8.0 4.0 10.0
one-arbitrary-number))
(newline)
(display
(estimate-integral
(inside-circle-predicate 0 0 1)
-1.0 1.0 -1.0 1.0
one-arbitrary-number))
(newline)
(define one-arbitrary-number 9999999)
(define (make-rand)
(let ((s (make-random-state)))
(lambda (m)
(cond ((eq? m 'generate) (random one-arbitrary-number s))
((eq? m 'reset) (lambda (new-s) (set! s new-s)))
(else (error "Unknown request -- RAND" m))))))
(define rand (make-rand))
(define s (make-random-state))
((rand 'reset) (make-random-state s))
(display (rand 'generate))
(newline)
(display (rand 'generate))
(newline)
(display (rand 'generate))
(newline)
((rand 'reset) (make-random-state s))
(display (rand 'generate))
(newline)
(display (rand 'generate))
(newline)
(display (rand 'generate))
(newline)
(define (make-account balance password)
(define pwd-list (list password))
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (add-password)
(lambda (new-pwd) (set! pwd-list (cons new-pwd pwd-list))))
(define (wrong-password . args) "Incorrect password")
(define (dispatch p m)
(cond ((not (member p pwd-list)) wrong-password)
((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
((eq? m 'add-password) add-password)
(else (error "Unknown request -- MAKE-ACCOUNT"
m))))
dispatch)
(define peter-acc (make-account 100 'open-sesame))
(display ((peter-acc 'open-sesame 'withdraw) 40))
(newline)
(display ((peter-acc 'rosebud 'deposit) 50))
(newline)
(define (make-joint acc password new-password)
((acc 'add-password password) new-password)
acc)
(define paul-acc (make-joint peter-acc 'open-sesame 'rosebud))
(display ((paul-acc 'open-sesame 'withdraw) 40))
(newline)
(display ((paul-acc 'rosebud 'deposit) 50))
(newline)
(display ((paul-acc 'open-sesame 'withdraw) 20))
(newline)
(define count -1)
(define (f x)
(set! count (+ count 1))
(if (= (remainder count 2) 0) 0 x))
(display (+ (f 0) (f 1)))
(newline)
(display (+ (f 1) (f 0)))
(newline)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment