15-212: Principles of Programming
We continue with streams, and complete our implementation by introducing a memoizing delay function. Memoization ensures that a suspended expression is evaluated at most once. When a suspension is forced for the first time, its value is stored in a reference cell and simply returned when the suspension is forced again. The implementation that we present makes a subtle and elegant use of a "self-modifying" code technique with circular references.
Here is the key stream code from Lectures 18 and 19 in two auxiliary files: stream.sml and mstream-io.sml.
[ CS 15-212 Home page | schedule | language | assignments | handouts ]