gpt4 book ai didi

programming-languages - 在方案或一般情况下使用的 'thunk' 是什么?

转载 作者:行者123 更新时间:2023-12-03 05:47:28 28 4
gpt4 key购买 nike

我在与Scheme和类似领域相关的代码和文档中的很多地方都遇到过“thunk”这个词。我猜测它是一个过程的通用名称,它有一个正式的参数。那是对的吗?如果是,还有更多吗?如果没有,请问?

例如。在 SRFI 18 ,在“程序”部分。

最佳答案

其实很简单。当您在程序中进行一些计算时,例如将 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。

惰性语言模糊了将变量绑定(bind)到值和创建返回该值的函数之间的界限,因此编写类似于上面第一种形式的内容实际上就像第二种形式一样。

关于programming-languages - 在方案或一般情况下使用的 'thunk' 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/925365/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com