Evaluation Order and Tail Positions

Follow the order in which Lispex evaluates calls and conditional expressions, see which expressions it skips, and find the tail positions.

You can predict a program's result by following the order in which it changes values and calls procedures. Lispex evaluates a call's operator first, then its operands from left to right. The last two sections list the tail positions and the resource limits that apply to loops.

Follow a changing value

Paste this complete program into the Playground.

LISPEX
(define x 0)
(list (begin (set! x 1) x) (begin (set! x (+ x 1)) x))

Result

OUTPUT
(1 2)

The first begin sets x to 1 and returns 1. The second reads that new value, adds one, stores 2, and returns it. The list procedure receives 1 followed by 2.

In a call, the operator is the expression that supplies the procedure. Here it is list. The operands are the expressions that supply its arguments, here the two begin expressions.

Follow each kind of expression

ExpressionEvaluation orderWhat happens to the results
procedure calloperator, then operands from left to rightafter checking the argument count, call the procedure
begin or a procedure bodyexpressions from left to rightdiscard earlier results and return the last result
iftest, then the selected branchreturn that branch's result
andoperands from left to rightstop at #f, or return the last result
oroperands from left to rightstop at the first result other than #f
letinitializers from left to right, using names from the enclosing scopebind the new names for the body
let*each initializer, then its bindinglater initializers can use earlier new bindings
letrecallocate storage for all names, then evaluate and assign initializers from left to rightall names are visible during initialization

An initializer supplies a name's starting value. Scope determines where a name is visible. The storage for one binding is called a cell. Reading a letrec cell before it has a value raises E321.

An error stops the current evaluation. The order above applies to the expressions reached before that error. Earlier begin and body expressions can return any number of values because their results are discarded.

See which expressions are skipped

Run this as a separate program.

LISPEX
(list (if #t 7 (/ 1 0))
      (and #f (/ 1 0))
      (or 0 (/ 1 0)))

Result

OUTPUT
(7 #f 0)

None of the divisions runs. The if selects 7, and stops at #f, and or stops at 0. Only #f is false, so 0 counts as true.

Argument checks happen after evaluation

An argument-count error does not prevent the operator and operands from running. With Native installed from Downloads, save this complete file as arity-effects.lspx.

LISPEX
((begin (display "operator ") (lambda (x) x))
 (begin (display "left ") 1)
 (begin (display "right") 2))
SH
lispex run arity-effects.lspx

Standard output contains the following text, with no final newline.

operator left right

The procedure expects one argument and receives two. The command then writes this diagnostic to standard error.

E302 arity-effects.lspx:1:1 arity mismatch: expected 1 argument(s), got 2

The exit code is 1, and no files are created. The procedure body does not run.

Locate tail positions

The examples that compare two ways to sum a list and repeat a loop ten thousand times are in the lesson Recursion and Tail Calls.

A call whose result becomes the calling procedure's result directly is in tail position. Lispex reuses the current continuation for such a call and does not add another pending call. This behavior is called proper tail calls.

Start with the last expression in a procedure body. When a form is itself in tail position, the following parts preserve that position.

FormTail position
let, let*, letrec, beginLast expression in the body
ifEither result branch
cond, caseLast expression in the selected clause
and, orLast operand
when, unlessLast expression in the body that runs
doRecursive step and last result expression
(apply proc args)Call to proc
(call-with-values producer consumer)Call to consumer

Tests and call arguments are evaluated before their results can be used. Those positions are not tail positions.

Expression in a procedure's tail positionIs the call to loop a tail call?
(if done? answer (loop next))Yes. Its result is returned directly.
(begin (display x) (loop next))Yes. It is the last expression.
(+ x (loop next))No. The addition still needs its result.
(map loop items)No. map collects the results of its calls to loop.
(apply loop args)Yes. apply preserves the tail position.

For call-with-values, the producer runs before the consumer. Only the consumer call inherits the tail position.

Account for resource limits

Tail calls keep pending call depth constant. The program still spends work on each iteration and may allocate data.

The selected resource profile, a set of execution limits, still applies. Non-tail recursion can reach a depth limit. A tail-recursive loop can reach another limit, such as its work budget.

For the same stated execution conditions, resource limits produce repeatable results. The command diagnostic names the limit reached with its code. A compare-engines comparison report also records the executor identifier, the resource profile, and the limit reached. See Repeatable Results and Execution Limits.

Names, Scope, and Changing Values explains the differences between the let forms. Writing Conditions covers branch selection.