sicp.io
2.2.1 · Sequence conventions

A list is a chain and a convention.

Pairs become sequences when a program agrees that each car holds one item and each cdr holds the rest of the list.

Guiding question

How do recursive procedures follow the shape of a list?

  • Read a list as first item plus remaining sequence
  • Identify the empty-list base case
  • Build a new list without mutating the inputs
  • Compare linked and indexed sequence access

length asks whether any sequence remains. Each nonempty pair contributes one and leaves the cdr for the smaller problem. The empty list stops the recursion.

append follows the same shape while rebuilding the left sequence. Its final cdr points to the right sequence, so the result preserves the order of both inputs.

A vector uses an indexed sequence convention. vector-ref selects an item by position, while vector-set! changes one position without rebuilding the whole vector.

SICP code280 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 (4 (a b c d)).

    Trace focus

    Watch cdr shorten the active problem. append creates one new pair per item in the left input and then reaches the right input. In the vector example, locate the update and later read at index 2.

    Try it yourself

    Change the program and compare the result.

    Define reverse using a helper that carries the result built so far. Test it with the list (a b c d).

    Show hint

    Move the car of the remaining input onto the front of an accumulator.

    Complete this lesson

    0 of 20 lessons complete in this chapter0%