15-150: Principles of Functional Programming

Lecture 21: Imperative Programming

The programming techniques used so far in the course have, for the most part, been purely functional. Some problems, however, are more naturally addressed by keeping track of the state of a machine. Typically this requires the use of mutable storage. SML supports mutable cells, or references, that store values of a fixed type. The value in a mutable cell can be initialized, read, and changed (mutated), and these operations result in effects that change the state. Programming with references is often carried out with the help of imperative techniques.

Imperative code makes it more difficult to reason about correctness, particularly for parallel implementations. However, imperative code with benign effects is often a good way to simplify design or improve efficiency. By "benign" we mean effects that are local (internal) to a function. From a client's perspective the code behaves just as would purely functional code.

Key Concepts

Sample Code

Some notes on imperative programming

Lecture Slides

Lecture Slides with every build, use "slideshow" option in your pdf viewer. Lecture Slides compact.