OCaml Koans
OCaml is a functional language with a strong, static type system that infers types for you, so programs are safe without being verbose. Its pattern matching, algebraic data types and module system make it a favorite for compilers, proof assistants, finance and systems tools. These koans cover its expressions and types, lists and pattern matching, records and variants, options and results, higher-order functions, modules and functors, and mutable state.
75 koans in 18 groups, tested with Alcotest. Each is a small program whose tests fail: replace every ___ until they pass.
Awakening
How a koan checks your answers, and how OCaml checks your types.
Numbers
Ints and floats, kept apart, and the functions that convert between them.
Strings and characters
Text, the characters it's made of, and formatting with Printf.
Functions
Defining functions, applying them, and functions of several arguments.
Recursion
Functions that call themselves, with let rec.
Lists
Immutable, singly linked lists, and the List module.
Pattern matching
match: taking values apart by their shape.
Tuples and records
Grouping values: by position, or by name.
Variants
Types that are one of several cases, and the matches that handle them all.
Options and results
Values that may be missing, and computations that may fail, in the types.
Higher-order functions
Functions that take and return functions.
Exceptions
Raising and handling errors.
Imperative OCaml
References, arrays and loops: OCaml lets you change things when you need to.
Modules
Grouping definitions, hiding them behind signatures, and functors.
Collections
Sets, maps and hash tables from the standard library.
Polymorphism
Code that works for many types, and the types that say so.
Laziness and sequences
Computing values only when they're needed.
Puzzles
Small programs to read and predict.