What is a 'thunk', as used in Scheme or in general?

Programming LanguagesFunctional ProgrammingScheme

Programming Languages Problem Overview


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.

Programming Languages Solutions


Solution 1 - Programming Languages

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.

Solution 2 - Programming Languages

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.

Solution 3 - Programming Languages

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser59634View Question on Stackoverflow
Solution 1 - Programming LanguagesSvanteView Answer on Stackoverflow
Solution 2 - Programming LanguagesSteven HuwigView Answer on Stackoverflow
Solution 3 - Programming LanguagesJohan KotlinskiView Answer on Stackoverflow