?? Learning Path

A guided order for studying the examples. Each stage builds on concepts from the previous one.

Stage 1 — Foundations

hello.ml — Start Here

OCaml infers types without annotations. The pipe operator chains transformations left-to-right, making data pipelines readable. No semicolons, no type declarations, no return keyword.

let bindings type inference pipe operator Printf
Stage 2 — Core Patterns

list_last_elem.ml — Safe List Traversal

OCaml uses Option instead of null. Pattern matching replaces if/else chains and is exhaustive — the compiler warns if you miss a case.

option types pattern matching recursion

factor.ml — Recursive Algorithms

Mutually recursive functions with let rec ... and ... — a natural way to express multi-phase algorithms. Shows input validation without exceptions in the happy path.

recursion mutual recursion input validation
Stage 3 — Data Structures

bst.ml — Algebraic Data Types

This is where OCaml shines. Model tree structures in 3 lines with algebraic data types. The 'a polymorphism means the tree works with any type. The accumulator pattern in inorder converts O(nē) to O(n).

algebraic data types polymorphism accumulators
Stage 4 — Higher-Order Functions

mergesort.ml — Functions as Arguments

Passing functions as arguments makes code reusable. Tail recursion makes it production-ready. Both split and merge are tail-recursive to handle large inputs without stack overflow.

higher-order functions tail recursion polymorphism
Stage 5 — Imperative OCaml

fibonacci.ml — Mutable State When You Need It

OCaml lets you use mutable state locally while keeping your public API pure. Closures encapsulate the hash table cache, making it invisible to the outside world. Demonstrates that choosing the right approach matters more than micro-optimization.

hash tables closures memoization benchmarking

What's Next?

  1. Modify an example — Add find to the BST, or make merge sort stable
  2. Write a new one — Implement a stack, queue, or graph traversal
  3. Read the manual — OCaml.org docs cover modules, functors, and the standard library
  4. Try Real World OCaml — dev.realworldocaml.org for production patterns