在计划或一般情况下使用的“thunk”是什么? [英] What is a 'thunk', as used in Scheme or in general?

查看:113
本文介绍了在计划或一般情况下使用的“thunk”是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在许多与Scheme有关的代码和文档以及类似的领域中,我遇到过很多地方的'thunk'这个词。我猜测这是一个程序的通用名称,它有一个正式的论点。那是对的吗?如果是的话,还有更多吗?如果没有,请吗?

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?

例如。在程序部分 SRFI 18 中。

推荐答案

它非常简单。当你有一些计算,比如在你的程序中加入3到5,然后创建它的一个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

在第二种情况下, foo 将被称为thunk。

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”是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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