15-150: Principles of Functional Programming

Lecture 2: Functions

We discuss functions.

We introduce the 5-step methodology for writing functions:

  1. In the first line of comments, write the name and type of the function.
  2. In the second line of comments, specify via a REQUIRES clause any assumptions about the arguments passed to the function.
  3. In the third line of comments, specify via an ENSURES clause what the function computes (what it returns).
  4. Implement the function.
  5. Provide testcases, generally in a format like this: val 6 = fact(3)

We introduce case expressions.

We introduce clausal function definitions based on pattern matching.

Key Concepts

  • Functions
  • Functions are values
  • case
  • Clausal function definitions
  • Pattern matching
  • 5-Step Methodology
  • Sample Code

    And here are the notes on evaluation again