CSE 341: Functions and patterns, supplementary notes

What is functional programming?

Functional programming is a style that emphasizes:

Secondary characteristics (usually, but not always part of FP) include:

Language Construct X in a Nutshell

All language constructs (in a statically typed language) have three parts:

Stating these three properties gives you the essence of the language construct. When language designers design a language, they go through these three steps for each construct --- either consciously or unconsciously.

In these notes, we will do two examples informally, to show this formula in action.

if/then/else in a Nutshell

Syntax:: If/then/else expressions have the syntactic form:

if expr1 then expr2 else expr3

Dynamic semantics: First, expr1 is evaluated to a value v. If v has the boolean value true, then expr2 is evaluated and returned. Otherwise, v has the boolean value false, and expr3 is evaluated and returned.

Static semantics:

Functions in a Nutshell

There are two constructs related to functions: definition (sometimes called abstraction in the programming languages literature) and application (a.k.a. function call).

Function definition

Syntax: Function definitions have the syntactic form

fn pattern => returnValue

Dynamic semantics: A function definition constructs a closure that contains two parts:

Static semantics:

Function application

Syntax: Function applications have the syntactic form

expr1 expr2

Dynamic semantics: First, expr1 is evaluated to a value value1. Second, expr2 is evaluated to a value value2. Then, value1's function body is evaluated in the environment produced by matching value2 against the argument pattern.

Static semantics:

* With polymorphic type variables appropriately instantiated. We'll learn about polymorphic types in the next couple of lectures.

Sample exercises

The answers to the following exercises are available here.