Scheme 中的标识符和绑定 - 如何解释函数?
我正在阅读 DrRacket 文档 http://docs.racket-lang.org/guide/binding。 html
有一个函数
(define f
(lambda (append)
(define cons (append "ugly" "confusing"))
(let ([append 'this-was])
(list append cons))))
> (f list)
'(this-was ("ugly" "confusing"))
我看到我们定义了函数f,里面我们定义了需要(append)的lambda,为什么? lambda 的过程(主体)是另一个名为 cons 的函数,它附加两个字符串。
我根本不明白这个功能。 谢谢 !
I am reading DrRacket document http://docs.racket-lang.org/guide/binding.html
There is a function
(define f
(lambda (append)
(define cons (append "ugly" "confusing"))
(let ([append 'this-was])
(list append cons))))
> (f list)
'(this-was ("ugly" "confusing"))
I see that we define function f, inside we define lambda that takes (append), why ?
Procedure (body) for lambda is another function called cons, that appends two strings.
I don't understand this function at all.
Thanks !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所指的部分演示了 Racket 中的词汇范围。与其他方案实现一样,要点是您可以“隐藏”语言中的每个绑定。与大多数“主流”语言不同,不存在真正的“神圣”关键字,因为它们永远不会被本地绑定所掩盖。
请注意,DrRacket 的“检查语法”按钮是一个非常好的可视化绑定内容的工具:单击它,您将看到代码中突出显示哪些部分是绑定,哪些是特殊形式 - 如果您将鼠标悬停在将鼠标悬停在特定名称上,您会看到一个箭头,告诉您它的来源。
The section that you're referring to demonstrates lexical scope in Racket. As in other Scheme implementations, the main point is that you can "shadow" every binding in the language. Unlike most "mainstream" languages, there are no real keywords that are "sacred" in the sense that they can never be shadowed by a local binding.
Note that a really good tool to visualize what is bound where is DrRacket's "check syntax" button: click it, and you'll see your code with highlights that shows which parts are bindings, which are special forms -- and if you hover the mouse over a specific name, you'll see an arrow that tells you where it came from.
方案需要一些时间来适应:)
f
被分配给lambda
返回的函数。lambda
定义带有参数的函数(称为append
)。(define cons (append "ugly" "confusing"))
本身不是一个函数,而是以两个字符串作为参数调用append,并将结果赋给cons。let
块内,append 被重新分配一个不同的值,即符号this-was
。append
列表(现在包含'this-was
)和cons
(其中包含'("ugly " "confusing")
来自上面的 3,f
f
使用参数调用list
(list
函数)作为参数append 传递,这就是为什么上面的3 创建了一个列表'("ugly" "confusing")。
被分配给cons
希望能澄清一些事情。
干杯!
Scheme takes some getting used to :)
f
is assigned the function returned by thelambda
.lambda
defines the function that takes a parameter (calledappend
).(define cons (append "ugly" "confusing"))
is not a function per se, but calls append with the two strings as parameter and assigns the result to cons.let
block, append is re-assigned a different value, the symbolthis-was
.append
(which now contains'this-was
) andcons
(which contains'("ugly" "confusing")
from 3 abovef
f
is called with the parameterlist
(thelist
function). which gets passed as the parameter append. And this is why 3 above creates a list'("ugly" "confusing")
which gets assigned tocons
.Hope that cleared up things a bit.
Cheers!