Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in
EPITA · Toulouse · 1st year (preparatory cycle)
Functional programming in Caml
An algorithmics seminar in Caml to start programming: definitions, types, functions, case analysis, imperative features and recursion.
The functional programming seminar I taught to first-year students at EPITA Toulouse in 2019-2020: six chapters and five exercise sheets.
The examples run in the page: the OCaml interpreter runs right here and prints its answer under each phrase. Edit them, run them again, break them: that is how you learn a language. For your own programs, the OCaml sandbox (in the menu, under “Free”) keeps your code in this browser. The first visit downloads the interpreter (about 2 MB); the following pages use it without waiting.
Lecture notes
- Introduction : What algorithmics is, what a programming language is, and why we start with a functional language.
- Caml basics : Definitions, basic types, expressions and the first predefined functions.
- Functions : Defining and applying functions, with one or several parameters, and reading their type.
- Case analysis : Choosing according to a condition or to the shape of a value. The conditional, exceptions, pattern matching, anonymous functions and tuples.
- Imperative features : Side effects, printing, the unit type, sequences and blocks.
- Recursion : Thinking up and writing a recursive function, from factorial to the towers of Hanoi, and measuring what it costs.
Exercise sheets
- Caml basics : Predicting the type and value of an expression, following definitions that hide one another, and using lazy operators and conversions. Chapter 1.
- Functions : Reading the type of a function, writing your first functions, placing parentheses, and discovering partial application. Chapter 2.
- Case analysis : Conditionals, exceptions, matching values and pairs, guards, and compiler warnings. Chapter 3.
- Imperative features : Predicting what a sequence displays, telling a procedure from a call to it, and placing begin … end. Chapter 4.
- Recursion : Writing recursive functions on integers, displaying with recursion, reasoning about termination and counting calls. Chapter 5.