OCaml is a statically typed functional language that also allows imperative and object-oriented code. It has strong type inference, fast native compilation and a pragmatic approach: strict evaluation and mutation when you need them. It is a favourite for writing compilers, theorem provers and static analysers, and for systems where types should catch mistakes.
Key concepts
- Type inference: types are checked at compile time without being written everywhere.
- Variants and pattern matching: data that is one of several cases, handled with exhaustiveness checks.
- Modules and functors: a rich module system that groups code and parameterises modules by other modules.
- Immutable by default, mutation allowed: records and lists are immutable; references and arrays are mutable.
- Strict evaluation: arguments are evaluated before a call, unlike lazy languages.
- Options and results: absence and failure are values, with exceptions kept for exceptional cases.
Vocabulary
- Variant: a type with several named constructors, also called a sum type.
- Functor: in OCaml, a module-level function from modules to modules.
- Dune and opam: the build tool and the package manager.
- Toplevel: the interactive prompt for trying code.
What to study, in order
- Expressions, let bindings, functions and recursion in the toplevel.
- Lists, tuples, records, variants and pattern matching.
- Higher-order functions, Option and Result, and exceptions.
- Modules and signatures; split a program into parts with interfaces.
- A project with a build tool: an interpreter for a small language with a parser and an evaluator.
- Functors and the standard library's data structures.