Programming Paradigms Session 4 · Wed 23 September 2026

← all sessions

Session 4

Functional Programming and Control

Functional programming and control: who decides the next step? Recursion and the stack, the loop as a value, evaluation order and parameter passing — then the λ-calculus as the formalism.

Lecture

Lab

This session

Who, and how, decides the next step of a computation? Block 1 asks it four times in real languages — at a function call, across an iteration, in the order and the moment arguments are evaluated — and each time sets beside the explicit control a functional way to express it: recursion, a function passed as the body of a loop, a function that delays an argument. Block 2 formalises those choices in the smallest functional language there is, the λ-calculus: three forms, one rule, and the strategy that picks the next redex.

The lecture, in two blocks

1Control patterns: recursion, the stack, fold, evaluation. Two definitions of computing (1936); a call on the machine and in the calculus; recursion and loops; the loop as a value; evaluation order and parameter passing. slides (web, with the demos) · PDF
2The formalism: the λ-calculus. Syntax, free variables, β-reduction, substitution; normal and applicative order, the small-step rules; booleans, numbers, if and recursion from three forms; laziness. slides (web, with the steppers) · PDF

The web slides run their code: Python in the page, JavaScript, and a λ-calculus stepper with a switch for the strategy. → moves one step; T lists the parts.

The questions of the session

Block 1: who decides the next step, in the languages we use?

The questionThe answer
What is computing? Two definitions, 1936. In the machine the next step is a function of the state; in the calculus it is a relation, and a strategy makes it a function.
What does a machine need to call a function? A frame. The stack holds what the calculus keeps in the term. Every loop can be written as a tail-recursive function, every recursion as a loop with a stack.
Do we need a loop construct at all? No. The recursion is written once, in fold; a use passes the body and writes no control. fold_left and fold_right differ in the order of applying f.
In which order, and when, are arguments evaluated? The language decides both: the order (fixed or unspecified) and the mode — by value, by name, by need. Under call by value if cannot be a function unless the caller delays the branches.

Each time, functional programming expressed the control through recursion and function values: recursion rather than an explicit stack, fold rather than a loop, a function that delays rather than an argument evaluated first. Implicit control is not particular to it: an imperative program also has a call stack and an evaluation order that the language fixes.

Block 2: block 1’s answers in one formalism

  • In the pure calculus, the order of reduction never changes the normal form. A term has at most one normal form, up to the renaming of bound variables (Church and Rosser, 1936). The order decides whether we reach it; normal order reaches it whenever it exists. (λx. λy. y) Ω gives λy. y in normal order, and runs for ever in applicative order.
  • A strategy says when an argument is evaluated, and each choice has a cost.
    StrategyAn argument is evaluatedWhat it costs
    call by valuebefore the call, oncework on arguments never used; Ω is fatal
    call by nameat each usethe same argument evaluated many times
    call by needat first use, oncea delayed argument kept, and updated when it is first used; memory held until then; and when evaluation has effects, their order is no longer readable from the program
  • The calculus has no ;, no while, no if. Control lives in values, and the values are functions: a boolean picks, a numeral repeats, Y recurses, a thunk delays. Functions as values are not a feature the calculus happens to have: they are what a language without control syntax must have, and they suffice.

Session 4 asked what the language decides: whether a call keeps a frame, in what order the arguments go, and when an argument is evaluated. It also showed the program taking these decisions itself, with an explicit stack, with fold, with branches wrapped in functions. Session 5 develops these techniques: iterators, thunks, streams.

Everything from functions

The calculus has three forms: a variable x, a function λx. t, an application t u. No numbers, no booleans, no let, no if. Block 2 defines them from the three forms:

(* a boolean is a choice: given two things, it picks one *)
true  = λt. λf. t
false = λt. λf. f
if    = λc. λt. λe. c t e

(* a number is a repetition: n applies f to x, n times *)
0    = λf x. x
3    = λf x. f (f (f x))
succ = λn f x. f (n f x)
add  = λm n f x. m f (n f x)

(* recursion without let rec *)
Y = λf. (λx. f (x x)) (λx. f (x x))
Z = λf. (λx. f (λv. x x v)) (λx. f (λv. x x v))
  • if only hands the two branches to the condition. It is a function under normal order; under call by value both branches are evaluated before the choice, and if is a function only when the caller delays them: block 1’s myIf, with each branch wrapped in a function.
  • A number applies a function n times: a number is its own fold, block 1’s fold taken literally.
  • Y g under call by value evaluates x x first, for ever. Z wraps it in a thunk, λv. x x v: the same repair as for if. OCaml refuses the type of Y before it can run it; why is session 10.

The types of the lab

The evaluator is three OCaml types and a few functions that match on them.

type term =
  | Var of string                 (* x     *)
  | Lam of string * term          (* λx. t *)
  | App of term * term            (* t u   *)

type strategy = CBN | CBV | Normal

type value = Closure of string * term * env
and  env = (string * value) list
  • term has one constructor per form of the calculus: the table of the slide Syntax: three forms, written as a type. The functions of the warm-up take a term apart with a match on these three cases.
  • strategy is the switch of the stepper, as a value. step matches on it and returns step_cbn, step_cbv or step_normal; the rules of each strategy are one match case each.
  • value and env are defined together, with and: an environment holds values, and a closure holds an environment. A function value is its parameter, its body, and the bindings it was made in. What the closure carries is session 5’s first question.

Two extensions change these types. A small ML adds Int, Add, If and Let to term, and the evaluator becomes a language; session 10 gives it a type checker. A meta-circular evaluator replaces Closure by VFun of (value -> value): a constructor that carries an OCaml function, so a λ of the calculus becomes a function of OCaml. readback shows what is lost.

The lab: warm-up, then the evaluator

WWarm-up. Five reductions on paper, then four functions on the term type: size, free_vars, is_closed, subst without capture. the sheet (PDF)
ASmall-step. One step under call by name, under call by value, and in full normal order; (λx. λy. y) Ω ends under one and not the other, in our own code.
BBig-step. An environment instead of substitution; a function value that carries the environment it was made in — a closure.

the lab page · README · source files: lambda.ml (the file we edit), term.ml, syntax.ml, main.ml · the whole folder. Nothing is handed in; the checks in main.ml say which parts remain.

Homework

  • Finish the lab, any part left open, in order; the extensions are on the lab page.
  • Reading. CS3110, chapter 10, sections 10.1–10.4; Pierce, Types and Programming Languages, chapter 5.

Languages this session

LanguageRole
OCamlread in both blocks, write in the lab — the evaluator for the calculus
λ-calculusread and run in the stepper on the slides, write on the warm-up sheet
Pythonread and run on the slides: count with and without a stack, Y and Z
JavaScriptread and run on the slides: the order in which arguments are evaluated, myIf with and without the branches as functions
C, Java, Scheme, Haskellread in the tables: tail calls, the loop as a value, the order of arguments, the parameter-passing modes

Further reading

None of this is required. Everything on the slides comes from the list on their last page; these are the parts of it that can be read online. Landin’s paper is on its publisher’s site, which may ask for a university login.

Two definitions of computing

  • Alan Turing, “On computable numbers, with an application to the Entscheidungsproblem” — 1936 (PDF). The machine. Its appendix shows that the machine computes the same functions as Church’s calculus.
  • John Backus, “Can programming be liberated from the von Neumann style?” — Turing Award lecture, 1978 (PDF). The case against programming one word of state at a time, by the designer of Fortran.

Recursion, the stack, tail calls

  • Guy Steele, “Debunking the ‘expensive procedure call’ myth” — MIT AI Memo 443, 1977. A procedure call is a jump that passes arguments; a properly implemented tail call keeps no frame for the caller.
  • William Clinger, “Proper tail recursion and space efficiency” — PLDI 1998 (PDF). What “proper tail calls” means, stated as a guarantee on space.

Evaluation strategies

  • Gordon Plotkin, “Call-by-name, call-by-value and the λ-calculus” — Theoretical Computer Science, 1975 (PDF). The two strategies of the lab, each with its own calculus.
  • John Hughes, “Why functional programming matters” — 1989 (PDF). Higher-order functions and lazy evaluation as two ways to glue programs together; the second is session 5.

Interpreters

  • Peter Landin, “The mechanical evaluation of expressions” — The Computer Journal, 1964. The first machine for the calculus with environments, and the closure: Part B of the lab.
  • John Reynolds, “Definitional interpreters for higher-order programming languages” — 1972, reprinted 1998 (PDF). An interpreter whose function values are functions of the language it is written in, as in the meta-circular extension, and how to turn them into records, as in Closure.