sicp.io
1.3.4 · One definition, two processes

A finite continued fraction can unfold or accumulate.

The same numerator and denominator procedures can drive a recursive process from the top or an iterative process from the final term.

Guiding question

How does evaluation order change the process without changing the finite fraction?

  • Express a finite continued fraction with numerator and denominator procedures
  • Read top-down recursive expansion as postponed division
  • Read bottom-up iteration as a complete accumulated result
  • Compare process shape while keeping the depth and terms fixed

The recursive version begins at term 1 but cannot finish its division until the rest of the fraction returns. Each call leaves one denominator addition and division waiting while recur moves toward k.

The iterative version begins at term k with 0.0 as the already-computed tail. Each call replaces result with one complete fraction layer and moves toward term 1. Both examples use ten numerators and denominators equal to 1.0, so they return the same finite approximation to the reciprocal golden ratio.

SICP code278 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 0.6179775280898876 for ten terms.

    Trace focus

    In the recursive run, follow recur toward k before the divisions return. In the iterative run, watch result become one complete suffix at each call while i decreases. The traces record the exact selected depth and terms.

    Try it yourself

    Change the program and compare the result.

    Change k from 10 to 5 in both programs. Predict whether they still agree and record the new finite value.

    Show hint

    Keep numerator and denominator unchanged. Only the number of fraction layers becomes smaller.

    Complete this lesson

    0 of 18 lessons complete in this chapter0%