Consult EOPL 2nd ed. text. Section 3.5 and Exercise 3.27 on Pages 84-91.
Exercise 3.27: When we build a closure, we have kept the entire environment in the closure. But of course all we need are the bindings for the free variables. Modify the interpreter to use the following definition of closure:
(define closure
(lambda (ids body env)
(let ((freevars (difference (free-vars body) ids)))
(let ((saved-env
(extend-env
freevars
(map (lambda (v) (apply-env env v)) freevars)
(empty-env))
))
(lambda (args)
(eval-expression body (extend-env ids args saved-env)))
)
)
))
where difference takes the difference of two sets. This is called the flat closure representation. The environment of such a closure consists of exactly
one rib comprising its free variables and their values.
That is, modify the interpreter code in myinterp.scm to support statically scoped user-defined procedures with the representation for closure as shown. Note that you need to supply the definition for free-vars and make any other necessary changes to incorporate the new definition of closure.
What to develop?
Create well-tested solutions in files named myClosure.rkt and myCompiler.rkt.
In order to better present the changes you made, comment out the
code you are modifying rather than deleting it in myClosure.rkt. Include test
calls and outcomes so as to make it clear
that you are exercising all the constructs.