koans.app

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.

Start the path

Awakening

How a koan checks your answers, and how OCaml checks your types.

  1. Checks
  2. Let bindings
  3. Types
  4. Equality

Numbers

Ints and floats, kept apart, and the functions that convert between them.

  1. Integers
  2. Floats
  3. Conversions
  4. The Int and Float modules

Strings and characters

Text, the characters it's made of, and formatting with Printf.

  1. Strings
  2. String functions
  3. Characters
  4. Printf

Functions

Defining functions, applying them, and functions of several arguments.

  1. Defining functions
  2. Anonymous functions
  3. Currying
  4. Labeled and optional arguments
  5. Pipelines

Recursion

Functions that call themselves, with let rec.

  1. Let rec
  2. Tail recursion
  3. Mutual recursion
  4. Classic recursion

Lists

Immutable, singly linked lists, and the List module.

  1. Building lists
  2. List functions
  3. Map and filter
  4. Folds
  5. Searching and sorting

Pattern matching

match: taking values apart by their shape.

  1. Match
  2. Matching lists
  3. Matching tuples
  4. Guards and or-patterns
  5. Patterns in let

Tuples and records

Grouping values: by position, or by name.

  1. Tuples
  2. Records
  3. Updating records
  4. Mutable fields

Variants

Types that are one of several cases, and the matches that handle them all.

  1. Enumerations
  2. Constructors with data
  3. Recursive variants
  4. Type parameters
  5. Checking your own types

Options and results

Values that may be missing, and computations that may fail, in the types.

  1. Options
  2. The Option module
  3. Results
  4. Chaining results

Higher-order functions

Functions that take and return functions.

  1. Functions as arguments
  2. Closures
  3. Composition
  4. Iterating for effects

Exceptions

Raising and handling errors.

  1. Raise and try
  2. Defining exceptions
  3. Standard exceptions
  4. Exceptions in match

Imperative OCaml

References, arrays and loops: OCaml lets you change things when you need to.

  1. References
  2. Arrays
  3. Loops
  4. Buffers

Modules

Grouping definitions, hiding them behind signatures, and functors.

  1. Modules
  2. Signatures
  3. Functors
  4. Your own functor
  5. Include

Collections

Sets, maps and hash tables from the standard library.

  1. Sets
  2. Maps
  3. Hash tables

Polymorphism

Code that works for many types, and the types that say so.

  1. Polymorphic functions
  2. Type annotations
  3. Polymorphic variants

Laziness and sequences

Computing values only when they're needed.

  1. Lazy values
  2. Sequences
  3. Unfolding

Puzzles

Small programs to read and predict.

  1. FizzBuzz
  2. Word frequencies
  3. Run-length encoding
  4. Roman numerals
  5. Quicksort