sicp.io
1.2.1 · Procedures and the processes they generate

The answer is only the last frame.

Two procedures can compute the same mathematical function while generating very different computational processes.

Guiding question

What makes a recursive process different from an iterative one?

  • Distinguish recursive syntax from recursive process growth
  • Recognize an explicit state carried by an iterative process
  • Use a fixed-limit execution trace to inspect process shape

The recursive version postpones multiplication until the smaller factorial returns. Its deferred work grows with n. The iterative version carries the partial product and counter as complete state.

Both definitions are recursive procedures because each calls itself. Only the second generates an iterative process whose state can be summarized by a fixed number of variables.

SICP code170 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

    Both programs return 40320.

    Trace focus

    Compare the order of calls and multiplications. The fixed-limit execution trace records the exact work performed by this selected input.

    Try it yourself

    Change the program and compare the result.

    Run both examples with 5. For the iterative version, write down n and product before each call.

    Show hint

    At every step, product times n factorial remains equal to the original factorial problem.

    Complete this lesson

    0 of 18 lessons complete in this chapter0%