sicp.io
4.1.2 · Control is part of meaning

A special form chooses what gets evaluated.

An evaluator cannot treat if like an ordinary procedure because it must select one branch before evaluating that branch.

Guiding question

What would go wrong if an evaluator evaluated both branches of if?

  • Recognize syntax with its own evaluation rule
  • Evaluate the predicate before selecting a branch
  • Observe that an unselected expression performs no work

The if case first evaluates only the predicate. It then calls evaluate on either the consequent or the alternative, never both.

In the first example the alternative divides by zero. The program still returns 60 because the true predicate selects the addition branch and the invalid alternative remains expression data.

SICP code975 of 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Execution trace0 / 0 events
    Programs run in the browser with their result and execution trace.
    Expected result

    The first program returns 60 without evaluating the division by zero.

    Trace focus

    After the less-than comparison returns true, follow evaluation into the addition branch. No division application appears in this run.

    Try it yourself

    Change the program and compare the result.

    Change score to 70 and replace the alternative with score. Predict which branch evaluate will visit.

    Show hint

    The host if inside evaluate enforces the evaluation rule of the interpreted if.

    Complete this lesson

    0 of 23 lessons complete in this chapter0%