Previous: , Up: baselib   [Index]


4.19 Tail calls and tail contexts

A tail call is a procedure call that occurs in a tail context. Tail contexts are defined inductively. Note that a tail context is always determined with respect to a particular lambda expression.

Certain built–in procedures must also perform tail calls. The first argument passed to apply and to call/cc, and the second argument passed to call-with-values, must be called via a tail call.

In the following example the only tail call is the call to f. None of the calls to g or h are tail calls. The reference to x is in a tail context, but it is not a call and thus is not a tail call.

(lambda ()
  (if (g)
      (let ((x (h)))
        x)
      (and (g) (f))))

NOTE Implementations may recognize that some non–tail calls, such as the call to h above, can be evaluated as though they were tail calls. In the example above, the let expression could be compiled as a tail call to h. (The possibility of h returning an unexpected number of values can be ignored, because in that case the effect of the let is explicitly unspecified and implementation–dependent.)


Previous: , Up: baselib   [Index]