Due: before class on Monday 17 November
Consider the following Scheme program:
(define x 10)
(define f
(lambda (x)
(display (+ x 20))
(newline)))
(f 3)
(f 4)
(define g
(lambda (a)
(lambda ()
(set! a (* a 2))
(f a))))
((g 5))
(define h (g 3))
(h)
(h)
-
Run the code in https://try.scheme.org/
What is the output from running this code?
-
Draw the frames and closures created from running this code, like we saw in class 4.2
(Hint: use a separate piece of paper. You should end up with 11 frames and 4 closures in total.)
-
Write equivalent Python code that does exactly the same thing.
-
Write a Scheme function
(make-account)to simulate a bank account.It should return another function (a
lambda) which accepts a command (either'depositor'withdraw) and an amount, updates the balance accordingly, and returns the new balance.Here is an example showing how it should work:
> (define rich (make-account)) > (rich 'deposit 1000000) 1000000 > (define poor (make-account)) > (poor 'deposit 10) 10 > (poor 'withdraw 100) -90 > (rich 'deposit 100) 1000100