73

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.

  • 2
    possible duplicate of [What is a 'thunk'?](http://stackoverflow.com/questions/2641489/what-is-a-thunk) – Stephan May 13 '14 at 09:09

3 Answers3

89

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.

Svante
  • 50,694
  • 11
  • 78
  • 122
  • 1
    Scheme is not a lazy language, right? (in 'Overview of Scheme' in r6rs). So, iff I create a think, like above, it will create a lazy evaluation scenario? –  May 29 '09 at 15:09
  • 1
    Please look at the link kotlinski (http://stackoverflow.com/questions/925365/what-is-a-thunk-as-used-in-scheme-or-in-general/925373#925373) provided for how Scheme implements a lazy evaluation scheme. – Svante May 29 '09 at 15:59
  • 1
    Yes. I did. However, my query here was: Will creating a lambda as above delay the evaluation in a non-Lazy language like Scheme? –  May 30 '09 at 11:17
  • 1
    Yes, of course. Why shouldn't it? Lazy just means that you don't have to do this explicitly, like the above. – Svante May 30 '09 at 11:48
  • I should add that a good lazy evaluation scheme (like the one in Scheme) also memoizes the result of a thunk. – Svante May 30 '09 at 11:51
  • You mean the one using 'delay' or the one using a thunk? And it happens automagically ? –  May 30 '09 at 13:14
  • The builtin delay/force mechanism automatically memoizes the result. What I showed above does exactly what it shows, nothing more. – Svante May 30 '09 at 14:53
  • @Svante what yo described is delayed or "lazy" evaluation, which is usually implemented as a thunk, but it's not a thunk in itself. Thunk is a more general idea which is used to implement more different ideas than just lazy-evaluated expressions. – SasQ Mar 21 '14 at 01:26
46

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.

Koneke
  • 178
  • 1
  • 9
Steven Huwig
  • 20,015
  • 9
  • 55
  • 79
20

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.

antoine
  • 115
  • 7
Johan Kotlinski
  • 25,185
  • 9
  • 78
  • 101
  • 4
    Thank You. I am looking for something better. Wikipedia doesn't explain things and concepts I really want to know. –  May 29 '09 at 10:37
  • 1
    @Amit -- I assume you commented before the question was updated with the quote. This is the exact answer to your question. – tvanfosson May 29 '09 at 10:41
  • 1
    @tvanfosson probablty it was edited after the original post. @kotlinski will it be possible to demonstrate it somehow? –  May 29 '09 at 10:47
  • 1
    OK, added an example... This is also explained well in Paradigms of Artificial Intelligence Programming by Peter Norvig (examine delay/force examples). – Johan Kotlinski May 29 '09 at 11:11
  • 1
    'force' and 'delay' seems to have been removed from the R6RS. –  May 30 '09 at 11:12
  • JavaScript Promise==thunk, or JavaScript Promise===thunk? :) – kayleeFrye_onDeck Apr 30 '17 at 20:16