在Scheme 或一般情况下使用的“thunk”是什么?

发布于 2024-07-22 15:03:42 字数 224 浏览 3 评论 0原文

我在与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绮筵 2024-07-29 15:03:42

这真的很简单。 当您在程序中进行一些计算时,例如将 3 与 5 相加,然后创建一个 thunk 意味着直接计算它,而是创建一个具有零参数的函数,该函数将在需要实际值。

(let ((foo (+ 3 5))) ; the calculation is performed directly, foo is 8
  ;; some other things
  (display foo)) ; foo is evaluated to 8 and printed

(let ((foo (lambda () (+ 3 5)))) ; the calculation is delayed, foo is a
                                 ; function that will perform it when needed
  ;; some other things
  (display (foo))) ; foo is evaluated as a function, returns 8 which is printed

在第二种情况下,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.

(let ((foo (+ 3 5))) ; the calculation is performed directly, foo is 8
  ;; some other things
  (display foo)) ; foo is evaluated to 8 and printed

(let ((foo (lambda () (+ 3 5)))) ; the calculation is delayed, foo is a
                                 ; function that will perform it when needed
  ;; some other things
  (display (foo))) ; foo is evaluated as a function, returns 8 which is printed

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.

以为你会在 2024-07-29 15:03:42

“thunk”是一个没有正式参数的过程对象,例如来自您的 SRFI 链接:

(lambda () (write '(b1)))

b1 变量绑定在封闭块中,这为我们提供了“thunk”一词的词源的线索,它依赖于一个笑话关于糟糕的语法。

零参数函数无法根据调用它的参数来更改其行为,因为它没有参数。 因此,函数的整个操作都已设置——它只是等待执行。 计算机不再需要“思考”,所有的“思考”都已经完成了——动作完全是“thunk”通过的。

这就是 SRFI 上下文中的所有“thunk”——一个没有参数的过程。

A "thunk" is a procedure object with no formal arguments, e.g. from your SRFI link:

(lambda () (write '(b1)))

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.

感情废物 2024-07-29 15:03:42

维基百科有以下答案:

在函数式编程中,“thunk”是空函数的另一个名称——不带参数的函数。 thunk 经常在严格语言中用作模拟惰性求值的手段; thunk 本身会延迟函数参数的计算,并且函数会强制 thunk 获取实际值。 在这种情况下,thunk 通常被称为暂停或(在Scheme 中)承诺。

添加 lazy方案中的评估示例。 在这里,promise 是 thunk 的另一种说法。

Wikipedia has the following answer:

In functional programming, "thunk" is another name for a nullary function — a function that takes no arguments. Thunks are frequently used in strict languages as a means of simulating lazy evaluation; the thunk itself delays the computation of a function's argument, and the function forces the thunk to obtain the actual value. In this context, a thunk is often called a suspension or (in Scheme) a promise.

Adding a lazy evaluation example in Scheme. Here, promise is another word for thunk.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文