Thunk是什么? 一头雾水

Section 2.9. Assignment of The Scheme Programming Language 中看到thunk这个概念,

Local state is sometimes useful for caching computed values or allowing a computation to be evaluated lazily , i.e., only once and only on demand. The procedure lazy below accepts a thunk , or zero-argument procedure, as an argument. Thunks are often used to “freeze” computations that must be delayed for some reason, which is exactly what we need to do in this situation. When passed a thunk t , lazy returns a new thunk that, when invoked, returns the value of invoking t . Once computed, the value is saved in a local variable so that the computation need not be performed again. A boolean flag is used to record whether t has been invoked and its value saved.

(define lazy
  (lambda (t)
    (let ([val #f] [flag #f])
      (lambda ()
        (if (not flag)
            (begin (set! val (t))
                   (set! flag #t)))
        val))))

上面这段描述, 读得一头雾水

The procedure lazy below accepts a *thunk* , or zero-argument procedure,
这里说thunk等同于 zero-argument procedure,
只要是没哟argument的procedure都是thunk?

维基百科里解释说thunk是在头昏脑涨凌晨2点钟编造的think的过去式.

你百度了吗?

http://www.ruanyifeng.com/blog/2015/05/thunk.html

看来维基百科也不靠谱

2 个赞

不错,学习了。

你能用scheme把博客里js的代码实现一遍吗?

如果不能 那我劝你放弃scheme

感谢,

是用来打底的,能用在日常的瑣碎思考中。

循路找到

约莫get到thunk的造字過程了。

Think這個動作會生成一個結果即idea
現在不直接用idea這個結果,而將think這個動作freeze保存住,
當再調用的時候,此think就成了過去的动作, 因此编造一个过去时态的thunk.