在Scheme 或一般情况下使用的“thunk”是什么?
我在与 Scheme 和类似领域相关的代码和文档中的很多地方都遇到过“thunk”这个词。 我猜测它是一个过程的通用名称,它有一个正式的参数。 那是对的吗? 如果是,还有更多吗? 如果没有,请问?
例如。 SRFI 18 中的“程序”部分。
I come across the word 'thunk' at a lot of places in code and documentation related to Scheme, and similar territories. I am guessing that it is a generic name for a procedure, which has a single formal argument. Is that correct? If yes, is there more to it? If no, please?
For eg. in SRFI 18, in the 'Procedures' section.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这真的很简单。 当您在程序中进行一些计算时,例如将 3 与 5 相加,然后创建一个 thunk 意味着不直接计算它,而是创建一个具有零参数的函数,该函数将在需要实际值。
在第二种情况下,
foo
将被称为 thunk。惰性语言模糊了将变量绑定到值和创建返回该值的函数之间的界限,因此编写类似于上面第一种形式的内容实际上在幕后被视为第二种形式。
It is really simple. When you have some computation, like adding 3 to 5, in your program, then creating a thunk of it means not to calculate it directly, but instead create a function with zero arguments that will calculate it when the actual value is needed.
In the second case,
foo
would be called a thunk.Lazy languages blur the line between binding a variable to a value and creating a function to return that value, so that writing something like the first form above is actually treated like the second, under the hood.
“thunk”是一个没有正式参数的过程对象,例如来自您的 SRFI 链接:
b1 变量绑定在封闭块中,这为我们提供了“thunk”一词的词源的线索,它依赖于一个笑话关于糟糕的语法。
零参数函数无法根据调用它的参数来更改其行为,因为它没有参数。 因此,函数的整个操作都已设置——它只是等待执行。 计算机不再需要“思考”,所有的“思考”都已经完成了——动作完全是“thunk”通过的。
这就是 SRFI 上下文中的所有“thunk”——一个没有参数的过程。
A "thunk" is a procedure object with no formal arguments, e.g. from your SRFI link:
The b1 variable is bound in the enclosing block, and this gives us a clue to the etymology of the word "thunk," which relies on a joke about poor grammar.
A zero-argument function has no way to change its behavior based on parameters it is called with, since it has no parameters. Therefore the entire operation of the function is set -- it is just waiting to be executed. No more "thought" is required on the part of the computer, all of the "thinking" has been done -- the action is completely "thunk" through.
That's all a "thunk" is in this SRFI's context -- a procedure with no arguments.
维基百科有以下答案:
添加 lazy方案中的评估示例。 在这里,promise 是 thunk 的另一种说法。
Wikipedia has the following answer:
Adding a lazy evaluation example in Scheme. Here, promise is another word for thunk.