Lambda and environmental model

I need help drawing the relevant parts of the environment model diagram when evaluating this code:

Scheme>(define x 10) Scheme> ((lambda (xy) (+ (y 3) x)) 6 (lambda (w) (* x 9))) 

I need to make sure and write each lambda body next to the environment in which it is evaluated.

It’s good that I know that there is only one definition, so most of the work will be done using "anonymous" or "nameless" functions, and they will still be displayed in various ways in the environment model diagram

+4
source share
2 answers

If I remember correctly, whenever you execute lambda, a new environment is created in which the values ​​of the arguments are bound to their names. This environment is inherited from any environment originally declared by lambda.

The first environment in all cases is the global environment - it is located here (define x 10) . Then, as I said, add a new environment when you execute lambda (as in the second line). This environment inherits from the environment in which the lambda was executed.

The first thing you did (starting from the second line) is the first lambda. To do this, you need to evaluate the arguments. Since you evaluate the arguments before the first entry into the first lambda, the second lambda is declared in the global environment.

Then an environment is created for the first lambda call (inheriting from the global environment). Here x is associated with 6 and y is associated with the second lambda. Then, to do + , the second lambda is called. Since it was declared in the global environment, its new environment inherits from this, and not from the first lambda environment. This means that for the second, x tied to 10, not 6.

Hope this explains everything clearly.

To clarify: there will be three environments - a global environment and one environment for each function call. Both function call environments inherit from the global environment. The first lambda code will run in its own environment, and the second lambda code will run the second lambda code.

Also check out the envdraw , which can be found here: http://inst.eecs.berkeley.edu/~cs3s/stk/site-scheme/envdraw/ If you read the ANNOUNCE file, it will tell you how to get it. You will need to use STk, a specific Schema interpreter.

envdraw automatically draws environment diagrams for the Scheme.

Disclaimer: I never bothered with envdraw when I took a class that used Scheme, but it was approved by my professor (apparently one of his students wrote it that day), and other people seem to be fine with it used.

+4
source

In addition to the answers already received, course 6.001 at the Massachusetts Institute of Technology has two very comprehensive lectures on the environmental model, the reasons for its existence, as well as some very useful and fine-grained step-by-step examples:

Lecture 1
Lecture 2

Hope this helps,

Jason

+3
source

All Articles