计划及浅连结 [英] Scheme and Shallow Binding

查看:121
本文介绍了计划及浅连结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 (define make (lambda (x) (lambda (y) (cons x (list y)))))

 (let ((x 7)
       (p (make 4)))
   (cons x (p 0)))

我新的计划和方案的功能,所以我有点笨重通过程序走,但我得到,如果我用深结合这一计划将返回(7 4 0)。说得通。你会这个程序做使用浅约束力?我得到这听起来愚蠢的,但在与利弊重新定义了一行P?因此,在这种情况下,我们将返回(7 0)?

I'm new to Scheme and functional program, so I am a bit clunky with walking through programs, but I get that if I used deep binding this program will return (7 4 0). Makes sense. What would this program do using shallow binding? I get this may sound dumb but is the p in the line with cons a redefinition? So in that case, we would return (7 0)?

基本上,我明白了深V的概念。浅约束力,但我觉得我混杂起来看方案时,因为我不是疯了熟悉它。

Basically, I understand the concept of deep v. shallow binding, but I feel like I'm jumbling it up when looking at Scheme because I'm not crazy familiar with it.

推荐答案

请参阅此维基百科的有关范围界定的讨论文章(它提到词法/动态范围和深/浅绑定)铭记计划是词法范围。将内斯的回答提供了更多信息。
现在,让我们来看看一步一步发生的事情在这个片段中code的:

See this wikipedia article for a discussion on scoping (it mentions lexical/dynamic scoping and deep/shallow binding) bearing in mind that Scheme is lexically scoped. Will Ness' answer provides additional information. For now, let's see step-by-step what's happening in this snippet of code:

; a variable called x is defined and assigned the value 7
(let ((x 7)
; make is called and returns a procedure p, inside its x variable has value 4
      (p (make 4)))
; 7 is appended at the head of the result of calling p with y = 0
  (cons x (p 0)))

=> '(7 4 0)

请注意,在第二行的闭合的在,由返回的拉姆达制造和变量 X创建内将被分配值 4 。这 X 无关与外 X ,因为方案是词法范围。

Notice that in the second line a closure is created in the lambda returned by make, and the variable x inside will be assigned the value 4. This x has nothing to do with the outer x, because Scheme is lexically scoped.

最后一行是不是重新定义,在previous段落内所提到的 X 制作 X定义在的前pression。

The last line is not a redefinition, as mentioned in the previous paragraph the x inside make is different from the x defined in the let expression.

这篇关于计划及浅连结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆