On this page

Type Inference and Higher-Order Functions

With core syntax, data structures, and pattern matching in place, this module turns to two ideas that shape how experienced OCaml code actually looks: understanding how the compiler figures out types without being told them, and treating functions themselves as ordinary values to be passed, returned, and combined. It also introduces OCaml's escape hatches from pure immutability — ref, mutable fields, and imperative loops — and explains why they tend to be used sparingly rather than by default.

Module Structure​

The module is organized into the following lessons or sections. Select one to explore its content. Each section is completely free to access and consult.

📄️Type Inference

An intuitive, no-proofs walkthrough of Hindley-Milner type inference: how OCaml figures out types without annotations.

📄️Parametric Polymorphism

How OCaml's parametric polymorphism works: 'a type variables, generic functions, and why OCaml has no operator overloading.

📄️Higher-Order Functions

How map, filter, fold_left, and fold_right work in OCaml, why they matter, and why fold_left and fold_right can disagree.

📄️Function Composition & Pipe Operator

How OCaml's |> and @@ operators work, how they differ from true function composition, and when to reach for each.

📄️Currying & Partial Application

Practical uses of currying and partial application in OCaml: specialized functions, pipelines, and a common warning to know.

📄️Mutable State

How OCaml's three mutable constructs work: ref cells, mutable record fields, and arrays, plus aliasing and safe patterns.

📄️Imperative Loops: For, While

How OCaml's for and while loops work, and the concrete reasons idiomatic code reaches for recursion and higher-order functions instead.

📄️Reading Unfamiliar Type Signatures

A systematic method for decoding unfamiliar OCaml type signatures and predicting what a function does before reading its code.

📄️Exercise: Build Your Own List Module

A guided, step-by-step project implementing map, filter, fold_left, fold_right, and sort using only pattern matching and recursion.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Exercise: Expression Evaluator

Next

Type Inference