Steele and Sussman | March 10, 1976 | 12 | LAMBDA: The Ultimate Imperative |

{Note Evalorder}

- In the usual applicative expression there are two implicit temporary values: those of
`(↑ B 2)`

and`(* 4 A C)`

. The first of these values must be preserved over the computation of the second, whereas the second is used as soon as it is produced. These facts are__manifest__in the appearance and use of the variable`X`

and`Y`

in the continuation-passing version.

In short, the continuation-passing version specifies __exactly__ and explicitly what steps are necessary to compute the value of the expression. One can think of conventional functional application for values as being an abbreviation for the more explicit continuation-passing style. Alternatively, one can think of the interpreter as supplying to each function an implicit default continuation of one argument. This continuation will receive the value of the function as its argument, and then carry on the computation. In an interpreter this implicit continuation is represented by the control stack mechanism for function returns.

Now consider what computation steps are implied by

(LAMBDA (A B C ...) (F X Y Z ...))

When we "apply" the `LAMBDA`

expression we have some values to apply it to; we let the names `A`

, `B`

, `C`

... refer to these values. We then determine the values of `X`

, `Y`

, `Z`

... and pass these values (along with "the buck", i.e. control!) to the lambda expression `F`

(`F`

is either a lambda expression or a name for one). Passing control to `F`

is an __unconditional transfer__. {Note Jrsthack} {Note Hewitthack}

Note that we want __values__ from `X`

, `Y`

, `Z`

... If these are simple expressions, such as variables, constants, or `LAMBDA`

expressions, the evaluation process is trivial, in that no temporary storage is required. In pure continuation-passing style, all evaluations are trivial: no combination is nested within another, and therefore no "hidden temporaries" are required. But if `X`

is a combination, say `(G P Q)`

, then we want to think of `G`

as a function, because we want a value from it, and we will need an implicit temporary place to keep the result while evaluating `Y`

and `Z`

. (An interpreter usually keeps these temporary places in the control stack!) On the other hand, we do __not__ necessarily need a value from `F`

This is what we mean by tail-recursion: `F`

is called tail-recursively, whereas `G`

is not. A better name for tail-recursion would be "tail-transfer", since no real recursion is implied. This is why we have made such a fuss about tail-recursion: it can be used for transfer of control without making any commitment about whether the expression expected to return a value. Thus it can be used to model statement-like control structures. Put another way, tail-recursion does not require a control stack as nested recursion does. In our models of iteration and imperative style all the `LAMBDA`

expressions used for control (to simulate `GO`

statements, for example) are called in tail-recursive fashion.