15-150: Principles of Functional Programming

Lecture 17: Functors, Parameterized Structures, Type Classes

A functor is a parameterized module that acts as a kind of function (not an ML function with a type, but a mathematical function). A functor takes a structure as argument and returns a new structure as result. Functors greatly facilitate hierarchical organization and code reuse in large programs. In particular, they can enable a clean separation between the details of a particular definition and a higher-level structure, allowing the implementation of "generic" algorithms that are easier to debug and maintain, and that maximize code reuse.

Key Concepts

The following perspective may be useful in thinking about types in a signature:

(Given a structure ascribing to a signature, a "client" is someone using that structure. The internals of the structure constitute the "implementation".)

Sample Code:    using transparent ascription      using opaque ascription

Some Notes on Functors      Some more Notes on Functors

Slides from Lecture