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