Lambda 和环境模型
在评估此代码时,我需要帮助绘制环境模型图的相关部分:
Scheme>(define x 10)
Scheme> ((lambda (x y) (+ (y 3) x)) 6 (lambda (w) (* x 9)))
我需要确保将每个 lambda 主体写在正在评估的环境旁边。
好吧,我知道只有一个定义,所以大部分工作将由“匿名”或“无名”函数完成,这些函数仍然会以各种方式显示在环境模型图中
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了已经给出的答案之外,麻省理工学院的 6.001 课程还有两场非常全面的讲座,内容涉及环境模型、其存在的原因,以及一些非常有用且细粒度的分步示例:
讲座 1
讲座 2
希望这会有所帮助,
杰森
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
如果我没记错的话,每当您执行 lambda 时,就会创建一个新环境,其中参数的值将与其名称绑定。该环境继承自 lambda 最初声明所在的环境。
所有情况下的第一个环境都是全局环境——这是
(define x 10)
所在的位置。然后,正如我之前所说,每当执行 lambda 时都添加一个新环境(如第二行所示)。该环境继承自执行 lambda 的环境。您所做的第一件事(从第二行开始)是调用第一个 lambda。为此,您必须评估论点。由于您在实际输入第一个 lambda 之前评估参数,因此第二个 lambda 是在全局环境中声明的。
接下来,为第一个 lambda 调用创建一个环境(继承自全局环境)。这里,
x
绑定到 6,y
绑定到第二个 lambda。然后,为了执行+
,调用第二个 lambda。由于它是在全局环境中声明的,因此它的新环境继承自此环境,而不是第一个 lambda 的环境。这意味着,对于第二个,x
绑定为 10 而不是 6。我希望这可以理解地解释一切。
澄清一下:将存在三个环境——全局环境和每个函数调用一个环境。两个函数调用的环境都将从全局环境继承。第一个 lambda 的代码将在其自己的环境中运行,而第二个 lambda 的代码将运行第二个 lambda 的代码。
此外,请查看
envdraw
,可在此处找到:http://inst.eecs.berkeley.edu/~cs3s/stk/site-scheme/envdraw/如果您阅读 ANNOUNCE 文件,它会告诉您如何获取它。您需要使用 STk,一种特定的方案解释器。
envdraw
自动为Scheme绘制环境图。免责声明:在上使用Scheme的课程时,我从来没有考虑过
envdraw
,但它得到了我的教授的认可(显然他的一个学生当天写回了它)并且其他人似乎使用得很好它。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 andy
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.