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 3-5.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.
In order for me to better grasp the changes you made, comment out the code you are modifying, rather than deleting it. Include test calls and outcomes in your turned in solution so as to make it clear that you are exercising all the constructs.
What to hand-in?
Submit your well-documented solution in file named 3-5-mod.scm by executing the following turn-in command on unixapps1.wright.edu. (You can optionally include a separate README.txt.)
%/common/public/tkprasad/cs784/turnin-pa2 3-5-mod.scm