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 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
- Functors
- Parameterized modules
- Substructures
- Code reuse
- Type Classes
The following perspective may be useful in thinking about types in a signature:
- concrete : client and implementation both know what the type is
- abstract : client does not/should not know how the type is implemented -- the details are left up to the implementation -- the client's code must work regardless of the implementation
- parameter : client supplies the type, implementation must work with whatever the client supplies
(Given a structure ascribing to a signature, a "client" is someone using that structure. The internals of the structure constitute the "implementation".)
Three files from the live coding in the 3pm lecture: queue queue-alt functors