Learnr

Learn OCaml

Let a strong type checker catch your bugs before the program ever runs.

Free to start. No credit card. Or browse all courses.

What's inside your OCaml course

A sample path Learnr builds for OCaml. Your real course adapts to your goal and level.

  1. Expressions, let bindings and type inference
  2. Functions, recursion, currying and partial application
  3. Lists, options, records and variants
  4. Pattern matching and exhaustiveness checking
  5. Modules, signatures and functors
  6. Mutable state, exceptions, tooling and a small project

OCaml is a functional language from the ML family with a strong static type system that infers types for you, so code stays short while the compiler still catches mistakes. Algebraic data types and exhaustive pattern matching make it a favorite for compilers, theorem provers and financial trading systems. It also allows mutation and imperative loops when you need them, and since version 5 it supports multicore parallelism. Learnr teaches OCaml with focused lessons and quick exercises, building from expressions and types up to modules and functors.

How to learn OCaml, step by step

  1. Install opam and explore in utop. Use opam to install the compiler, dune and the utop REPL. Type expressions into utop and read the type it prints for each one, which is the fastest way to build intuition for inference.
  2. Learn the arithmetic and typing quirks early. Integers and floats use different operators, so 1.5 +. 2.0 works while 1.5 + 2.0 is a type error. Explicit conversions like float_of_int feel strict at first, and they remove a whole category of silent bugs.
  3. Model problems with variants. Define a type for shapes or JSON values as a variant and write functions that match on every constructor. Turn on warnings and let the compiler point out cases you forgot to handle.
  4. Use option instead of null. OCaml has no null. Practice returning Some x or None and handling both cases, and learn Option.map and Option.value so the code stays tidy rather than nesting matches.
  5. Graduate to modules and functors. Write a module signature for a set or a stack, implement it twice, then build a functor such as Map.Make applied to your own key type. This is where OCaml differs most from other functional languages.
  6. Build an interpreter as a capstone. Write a tokenizer, parser and evaluator for a tiny arithmetic language. Variants and pattern matching make this exactly the kind of program OCaml shines at, and it ties every earlier topic together.

What you'll learn

  • Read inferred type signatures and fix type errors from the compiler
  • Model data with records and variants and handle every case with match
  • Write recursive and higher-order functions over lists and options
  • Structure code with modules, signatures and functors
  • Set up projects with opam and dune and explore code in utop

How long does it take to learn OCaml?

With prior programming experience, plan on 4 to 8 weeks at 4 to 6 hours a week to write small programs confidently. Modules and functors usually take the longest. If functional programming is entirely new, add a few weeks for recursion and immutability.

What you need before you start

Some programming experience in any language is helpful. No functional programming background is needed, though comfort with recursion will make the first lessons faster.

Common mistakes to avoid

  • Fighting type errors by guessing instead of reading them. The message names the expected and actual types, so trace back to where they diverge.
  • Using lists for everything and calling List.nth in loops, which is slow. Reach for arrays, maps or hashtables when you need indexed or keyed access.
  • Silencing the non-exhaustive match warning with a wildcard case, which hides the exact bug the type system was trying to show you.

How Learnr builds your course

Type your topic

Tell Learnr you want to learn OCaml, or get as specific as you like.

Get a personalized course

Learnr instantly builds a full OCaml course tailored to your goal and skill level.

Learn and level up

Move through bite-sized lessons and interactive practice, earning XP and keeping your streak.

OCaml questions, answered

Is OCaml used in industry?

Yes, though in a focused set of places. Jane Street uses it for trading systems, and it appears in compilers, static analyzers, blockchain projects and tools such as the Rocq proof assistant (formerly Coq) and the Flow type checker from Meta.

How does OCaml compare to Haskell?

Both are typed functional languages with inference and pattern matching. OCaml is strictly evaluated and allows side effects directly, which many find easier to reason about, while Haskell is lazy and tracks effects in its type system.

What is the best free resource alongside a course?

The book Real World OCaml is available free online and covers the language and its standard tooling in depth. The official ocaml.org site also has tutorials and documentation for the standard library.

Explore more courses

More programming and tech courses

See all programming and tech courses

Ready to learn OCaml?

Start your free, personalized OCaml course in seconds. It will be typed in and ready when you get there.

Start learning OCaml

Free to start. No credit card.