15-150: Principles of Functional Programming

Lecture 4: Recursion and Structural Induction on Lists, Tail Recursion

We introduced SML's predefined list datatype for integers during lab last week and again during the previous lecture.
An important point is that the list datatype is defined recursively:
An   int list   is one of the following (and nothing else):

  1. Either the empty list (written nil or []),
  2. or of the form  x::xs, with   x : int   and   xs : int list.

Last time and today we wrote some functions taking list arguments and/or returning list values, and we showed how to prove theorems about lists using structural induction.

We discussed tail recursion, a form of recursion that is somewhat like the use of loops in imperative programming. This form of recursion is often used to make functions more efficient. Accumulator arguments play an important role in tail recursion. The presences of an accumulator argument in a function forces us to think carefully about the specification of the function and to prove corresponding theorems of correctness.

Key Concepts

Sample Code

Some Notes on Structural Induction

(These notes also discuss structural induction over trees; we will see trees in the next lecture.)