This preview shows page 1 out of 4 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 4 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CS 61A Summer 2010 Week 4B LabWednesday 7/14 Afternoon1. Given below is a simplified version of the make-account procedure on page 223 of Abelson andSussman.(define (make-account balance)(define (withdraw amount)(set! balance (- balance amount)) balance)(define (deposit amount)(set! balance (+ balance amount)) balance)(define (dispatch msg)(cond((eq? msg ’withdraw) withdraw)((eq? msg ’deposit) deposit) ) )dispatch)Fill in the blank in the following code so that the result works exactly the same as the make-accountprocedure above, that is, responds to the same messages and produces the same return values. Thedifferences between the two procedures are that the inside of make-account above is enclosed in thelet below, and the names of the parameter to make-account are different.(define (make-account init-amount)(let ( )(define (withdraw amount)(set! balance (- balance amount)) balance)(define (deposit amount)(set! balance (+ balance amount)) balance)(define (dispatch msg)(cond((eq? msg ’withdraw) withdraw)((eq? msg ’deposit) deposit) ) )dispatch) )2. Modify either version of make-account so that, given the message balance, it returns the currentaccount balance, and given the message init-balance, it returns the amount with which the accountwas initially created. For example:> (define acc (make-account 100))acc> (acc ’balance)1003. Modify make-account so that, given the message transactions, it returns a list of all transactionsmade since the account was opened. For example:> (define acc (make-account 100))acc> ((acc ’withdraw) 50)150> ((acc ’deposit) 10)60> (acc ’transactions)((withdraw 50) (deposit 10))4. Given this definition:(define (plus1 var)(set! var (+ var 1))var)Show the result of computing(plus1 5)using the substitution model. That is, show the expression that results from substituting 5 for var inthe body of plus1, and then compute the value of the resulting expression. What is the actual resultfrom Scheme?This lab activity consists of example programs for you to run in Scheme. Predict the result beforeyou try each example. If you don’t understand what Scheme actually does, ask for help! Don’twaste your time by just typing this in without paying attention to the results.(define (make-adder n) ((lambda (x)(lambda (x) (+ x n))) (let ((a 3))(+ x a)))(make-adder 3) 5)((make-adder 3) 5) (define k(let ((a 3))(define (f x) (make-adder 3)) (lambda (x) (+ x a))))(f 5) (k 5)(define g (make-adder 3)) (define m(lambda (x)(g 5) (let ((a 3))(+ x a))))(define (make-funny-adder n)(lambda (x) (m 5)(if (equal? x ’new)(set! n (+ n 1)) (define p(+ x n)))) (let ((a 3))(lambda (x)(define h (make-funny-adder 3)) (if (equal? x ’new)2(set! a (+ a 1))(define j (make-funny-adder 7)) (+ x a)))))(h 5) (p 5)(h 5) (p 5)(h ’new) (p ’new)(h 5) (p 5)(j 5) (define r(lambda (x)(let ((a 3)) (let ((a 3))(+ 5 a)) (if (equal? x ’new)(set! a (+ a 1))(let ((a 3)) (+ x a)))))(lambda (x) (+ x a)))(r 5)((let ((a 3))(lambda (x) (+ x a))) (r 5)5)(r ’new)(r 5)(define s (define (ask obj msg . args)(let ((a 3)) (apply (obj msg) args))(lambda (msg)(cond ((equal? msg ’new) (ask s ’add 5)(lambda ()(set! a (+ a 1)))) (ask s ’new)((equal? msg ’add)(lambda (x) (+ x a))) (ask s ’add 5)(else (error "huh?"))))))(define x 5)(s ’add)(let ((x 10)(s ’add 5) (f (lambda (y) (+ x y))))(f 7))((s ’add) 5)(define x 5)(s ’new)3((s ’add) 5)((s ’new))((s ’add) 5)5. What will the final expression in the following program return? Try to figure this out on your ownbefore using the interpreter.(define answer 0)(define (square f x)(let ((answer 0))(f x) answer))(square (lambda (n) (set! answer (* n n)))


View Full Document

Berkeley COMPSCI 61A - Lab 4

Documents in this Course
Lecture 1

Lecture 1

68 pages

Midterm

Midterm

5 pages

Midterm

Midterm

6 pages

Lecture 35

Lecture 35

250 pages

Lecture 14

Lecture 14

125 pages

Lecture 2

Lecture 2

159 pages

Lecture 6

Lecture 6

113 pages

Lecture 3

Lecture 3

162 pages

Homework

Homework

25 pages

Lecture 13

Lecture 13

117 pages

Lecture 29

Lecture 29

104 pages

Lecture 11

Lecture 11

173 pages

Lecture 7

Lecture 7

104 pages

Midterm

Midterm

6 pages

Midterm

Midterm

6 pages

Lecture 8

Lecture 8

108 pages

Lecture 7

Lecture 7

52 pages

Lecture 20

Lecture 20

129 pages

Lecture 15

Lecture 15

132 pages

Lecture 9

Lecture 9

95 pages

Lecture 30

Lecture 30

108 pages

Lecture 17

Lecture 17

106 pages

Load more
Download Lab 4
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lab 4 and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lab 4 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?