15-212: Principles of Programming

Lecture 19: Streams and Memoization

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.

Key Concepts

Sample Code

Here is the key stream code from Lectures 18 and 19 in two auxiliary files: stream.sml and mstream-io.sml.

On-line Notes by Robert Harper

Suggested Reading in Programming in Standard ML:

[ CS 15-212 Home page | schedule | language | assignments | handouts ]

Michael Erdmann