?? Learning Path
A guided order for studying the examples. Each stage builds on concepts from the previous one.
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.
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.
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.
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).
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.
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.
What's Next?
- Modify an example Add
findto the BST, or make merge sort stable - Write a new one Implement a stack, queue, or graph traversal
- Read the manual OCaml.org docs cover modules, functors, and the standard library
- Try Real World OCaml dev.realworldocaml.org for production patterns