Learn faster and stay on-track by joining this free class with other self-learners.
Register for Structure and Interpretation of Computer Programs now.
|
Structure and Interpretation of Computer ProgramsClass length: 13 weeks. Start anytime. Creator: kday Status: Established |
Join this class! |
|
Lesson 9: Assignment 1Do all problems in section 3.1. Total of 8 problems. Homework Submissions1 totalAfter lesson 8 these exercises were very basic. Also I switched to using the racket language to use the built in random number generator and seeding functions rather than writing my own for the sake of the monte-carlo exercises. The only other interesting thing was the 3.08 - how to determine if argument are evaluated left-to-right or right-to-left. The trick is to use some local state to remember if a zero has been already been passed. #lang racket
;; Ex 3.01
;;
(define (make-accumulator delta)
(lambda (amount)
(set! delta (+ delta amount))
delta))
;; Ex 3.02
;;
(define (make-monitored proc)
(let ((calls 0))
(lambda (arg)
(cond ((eq? arg 'how-many-calls?) calls)
((eq? arg 'reset-count) (set! calls 0) calls)
(else (set! calls (+ calls 1))
(proc arg))))))
;; Ex 3.03
;;
(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 (dispatch key m)
(cond ((not (eq? key password))
(error "Incorrect password -- MAKE-ACCOUNT"))
((eq? m 'withdraw) withdraw)
((eq? m 'deposit) deposit)
(else (error "Unknown request -- MAKE-ACCOUNT" m))))
dispatch)
;; Ex 3.04
;;
(define (call-the-cops)
(display "Calling the cops")
(error "Too many bad passwords"))
(define (make-alarm-account balance password)
(define bad-passwords 0)
(define (withdraw amount)
(if (>= balance amount)
(begin (set! balance (- balance amount))
balance)
"Insufficient funds"))
(define (deposit amount)
(set! balance (+ balance amount))
balance)
(define (dispatch key m)
(cond ((not (eq? key password)) (set! bad-passwords (+ bad-passwords 1))
(if (> bad-passwords 7)
(lambda (v) (call-the-cops))
(lambda (v) "Incorrect password")))
((eq? m 'withdraw) (set! bad-passwords 0)
withdraw)
((eq? m 'deposit) (set! bad-passwords 0)
deposit)
((eq? m 'balance) balance)
(else (error "Unknown request -- MAKE-ACCOUNT" m))))
dispatch)
;; Ex 3.05
;;
(define (rand-update x) (random (expt 2 31)))
(define (random-init) (rand-update 0))
(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))
(define (square x) (* x x))
(define (random-in-range low high)
(let ((range (- high low)))
(+ low (random range))))
(define (estimate-integral x1 y1 x2 y2 pred? trials)
(monte-carlo trials
(lambda ()
(pred? (random-in-range x1 x2)
(random-in-range y1 y2)))))
(define (estimate-integral-pi trials)
(define radius 1000)
(define (circle-test x y)
(<= (+ (square x)
(square y))
(square radius)))
(exact->inexact
(* 4
(estimate-integral
(- radius) (- radius) radius radius
circle-test
trials))))
;; Ex 3.06
;;
(define initial-seed 317)
(define (rand-update-seed x)
(random (expt 2 31)))
(define (random-init-seed)
(rand-update-seed (random-seed initial-seed)))
(define rand-seed
(let ((x (random-init-seed)))
(lambda (action)
(cond ((eq? action 'generate)
(set! x (rand-update-seed x))
x)
((eq? action 'reset)
(lambda (new-seed)
(rand-update-seed (random-seed new-seed))))))))
;; Ex 3.07
;;
(define (make-joint acc password joint-password)
(define (dispatch key m)
(cond ((not (eq? key joint-password))
(error "Incorrect password -- MAKE-JOINT"))
((eq? m 'withdraw) (acc password 'withdraw))
((eq? m 'deposit) (acc password 'deposit))
(else (error "Unknown request -- MAKE-JOINT" m))))
dispatch)
;; Ex 3.08
;;
(define f
(let ((seen-zero? #f))
(lambda (x)
(cond (seen-zero? 0)
(else (set! seen-zero? #t)
x)))))
|
No comments. Sign up or log in to comment