OCaml algebraic types and pattern matching, step by step

8 steps · about 5 h 30 min · Beginner

  1. Basic Data Types and Pattern Matching (doc, ocaml.org, 25 min)

    The official overview of built-in types, records, variants and pattern matching, so you start with the correct vocabulary.

  2. 3.2. Variants — OCaml Programming: Correct + Efficient + Beautiful (article, Cornell CS 3110, 20 min)

    This step introduces constructors and how a match takes a variant apart, starting from simple enumerations.

  3. A Guided Tour - Real World OCaml (article, Real World OCaml, 40 min)

    Run records and variants in the toplevel, including the shape-scene example, to see how types model a problem.

  4. 3.9. Algebraic Data Types — OCaml Programming: Correct + Efficient + Beautiful (article, Cornell CS 3110, 30 min)

    Covers variants that carry data, recursive and parameterized variants, and the catch-all warning.

  5. Algebraic data types - CS 242 (article, Stanford CS 242, 20 min)

    Shows option and result types, which make error handling impossible to forget.

  6. Variants - Real World OCaml (article, Real World OCaml, 45 min)

    Goes deeper on exhaustiveness checks, why catch-all cases hide bugs, and how variants help with refactoring.

  7. 3.4. Exercises · Functional Programming in OCaml (tutorial, Cornell CS 3110, 1 h)

    Practice pattern matching on lists, options and trees, and write your own variant types.

  8. 99 problems in OCaml (repo, MassD (GitHub), 1 h 30 min)

    Extra drills on lists and other structures, using patterns and option types, to make the skill automatic.