(Lispex)sicp.io
2.4 · Identity and mutation

Shared structure makes mutation visible at a distance.

Two paths can lead to the same pair. Mutating that pair through either path changes what both paths observe.

Guiding question

Why is shared identity different from merely equal contents?

  • Distinguish one shared object from two equal objects
  • Predict the effect of mutation through an alias
  • Read shared references in a graph-shaped result

tree stores shared in both its car and cdr. There is one inner pair and two references to it, not two copied lists. set-car! therefore changes the object reached from both sides.

The printed #0= label introduces the shared object and #0# refers to that same object again. The labels preserve identity that ordinary list notation would hide.

Lispex · SICP sourceScheme-compatible SICP syntax executed by the Lispex SICP profile.
(let ((shared (list 'leaf)))
  (let ((tree (cons shared shared)))
    (set-car! shared 'changed)
    tree))
Lispex learning runtimeLispex SICP profile 1.0.0
Loading Lispex SICP runtime
Lispex · SICP source107 / 1,048,576 UTF-8 bytes
Examples
Result
Output
Value
Diagnostic
Visible execution0 / 0 trace events
    This browser result is not a Lispex Vouch record or authority.wasm —
    Expected observation

    The first program returns (#0=(changed) . #0#).

    Trace focus

    Look for one allocation of the shared inner pair and one later mutation. The result graph keeps both references attached to the same object identity.

    Try it yourself

    Change the program before you read the hint.

    Run the separate-pairs example and compare its result. Explain why changing left does not change right even though both began with equal contents.

    Show one hint

    Count allocations, not printed leaf symbols.