One recurrence can appear as a changing object or an immutable stream.
Generate the same deterministic values through a private mutable seed and through a stream whose next state is an explicit delayed argument.
Which dependencies become visible when state is represented as a stream instead of hidden inside an object?
- Implement one recurrence with a stateful generator object
- Implement the same recurrence as an immutable delayed stream
- Compare equal finite observations across two process structures
- Reset an object by mutation and restart a stream by reconstruction
- Choose a model according to the interfaces and histories a client needs
make-rand hides seed and changes it on every generate message. random-stream instead receives the current seed explicitly, computes next, places it in the stream, and delays construction from that next value. Both use the same recurrence and therefore expose the same first five numbers from seed 1.
The resettable object changes its private seed in place. The functional version restarts by constructing a new stream from seed 1; the old stream remains available. Equal result lists establish agreement for this finite prefix only. The object emphasizes identity and commands, while the stream emphasizes reusable histories and explicit data flow.
- Output
- —
- Value
- —
- Diagnostic
- —
The first program returns the sequence (48271 182605794 1291394886 1914720637 2078669041) twice, followed by #t. The reset program returns (48271 182605794 reset 48271 48271 182605794).
Follow set! into the private object seed and compare it with the explicit next argument captured by each stream promise. In the reset run, distinguish mutating one object identity from constructing and retaining a separate stream history.
Change the program and compare the result.
Map both models into a finite stream or list of even? observations, then branch the functional stream into two consumers while continuing the single object. Explain which histories can be replayed without resetting.
Show hint
A memoized stream tail can be shared by several consumers. A stateful object has one current location unless its protocol explicitly stores history.