typestar

Language basics

29 steps in 9 sets of OCaml.

OCaml is the industrial branch of the ML family: type inference so complete that annotations are a style choice, pattern matching the compiler audits for coverage, and a module system — functors, signatures — that no mainstream language has matched. Jane Street trades billions through it, Coq and the original Rust compiler were written in it, and F#, ReasonML, and a generation of type-checker authors carry its ideas. It is fast, terse, and honest: absence is an option, errors are values or declared exceptions, and mutation is spelled out.

This tour types the core: let and let-in with refs as the loud exception, Printf, currying and partial application, the pipeline and @@ and hand-rolled composition, labeled and optional arguments, match with guards, option and result and variants, cons lists with the List module and both folds, records with copy-and-update, tuples, polymorphism, Hashtbl against assoc lists, mutable arrays with the dotted float operators, lazy Seq, string processing, and the signature pieces — modules and functors via Map.Make. The Encore is the cross-language script trio in honest stdlib OCaml.

Start this tour

Values

Functions

  • CurryingEvery function takes one argument; partial application is free.
  • The pipeline|> feeds the left value forward; data reads in flow order.
  • Composition and @@Build functions from functions; @@ drops the last parens.
  • Labeled arguments~labels name arguments; ?optionals carry defaults.

Pattern matching

  • matchAn expression the compiler audits for coverage.
  • optionAbsence spelled Some or None — the type that killed null.
  • VariantsA closed set of shapes; match must cover them all.

Lists

  • Lists and consImmutable and linked: :: builds, @ appends, patterns split.
  • The List modulemap, filter, sort, fold_left: the daily toolkit.
  • Foldsfold_left threads an accumulator of any shape.
  • GroupingGroup by key with a fold into an assoc list, then rank.

Types

  • RecordsImmutable named bundles with structural equality built in.
  • TuplesPair values without ceremony; patterns take them apart.
  • Polymorphism'a is any type: one function, checked once, reused everywhere.

Collections

  • Hashtbl and assoc listsThe mutable map, and the immutable habit for small data.
  • ArraysFixed-size and mutable: .(i) indexes, <- writes.
  • SeqThe lazy sequence: infinite until something pulls.
  • String processingsplit_on_char and filter_map turn field notes into data.

Modules

  • ModulesGroup values and types; the dot reaches inside.
  • FunctorsModule-level functions: Map.Make builds a map from an ordering.

Idioms

  • resultOk or Error, with bind to keep the pipeline on the happy track.
  • ExceptionsDeclared like variants, caught by pattern.
  • Recursionrec allows self-reference; accumulators keep calls in tail position.

Encore

  • word_freq.mlA word-frequency table from split_on_char, a Hashtbl, and one sort.
  • grade_report.mlReport cards from a record roster, an if ladder, and printf columns.
  • temp_stats.mlA week of temperatures: fold-picked extremes, a bar chart, swings.

The other OCaml tours