Lambda and the Environment Model

為{幸葍}努か 提交于 2020-01-22 21:31:08

问题


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

Scheme>(define x 10)
Scheme> ((lambda (x y) (+ (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 being evaluated.

Okay I know that there is only one define so most of the work will be done by “anonymous” or “nameless” functions and these will still show up in various ways in the environment model diagram


回答1:


In addition to the answers already given, the 6.001 course at MIT has two very comprehensive lectures on the environment model, the reasons for its existence, as well as some very helpful and fine-grained step-by-step examples:

Lecture 1
Lecture 2

Hope this helps,

Jason




回答2:


If I remember correctly, whenever you execute a lambda, a new environment is created where the arguments' values are bound to their names. This environment inherits from whichever environment the lambda was originally declared in.

The first environment in all cases is the global environment--this is where the (define x 10) resides. Then, as I said before, add a new environment whenever you execute a lambda (as in the second line). This environment inherits from whichever environment the lambda was executed in.

The first thing you did (starting with the second line) is call the first lambda. To do this, you have to evaluate the arguments. Since you evaluate the arguments before actually entering the first lambda, the second lambda is declared in the global environment.

Next, an environment is created for the first lambda's call (inheriting from the global environment). Here x is bound to 6 and y is bound to the second lambda. Then, to do the +, the second lambda is called. Since it was declared in the global environment, its new environment inherits from this rather than from the first lambda's environment. This means that, for the second one, x is bound to 10 rather than 6.

I hope this explains everything understandably.

To clarify: there are going to be three environments--the global environment and one environment per function invocation. Both of the function invocations' environments will inherit from the global environment. The first lambda's code will run in its own environment, while the second lambda's code will run the the second lambda's.

Additionally, check out 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'll need to use STk, a particular Scheme interpreter.

envdraw draws environment diagrams for Scheme automatically.

Disclaimer: I never bothered with envdraw when taking the class that used Scheme, but it was endorsed by my professor (apparently one of his students wrote it back in the day) and other people seemed to do fine using it.



来源:https://stackoverflow.com/questions/5845340/lambda-and-the-environment-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!