Course

OCaml from the ground up

An expression language with types that catch you early

  • Beginner
  • 12 chapters
  • 222 min read

Learn OCaml by writing it: values and printing, names with let, functions and recursion, decisions that are expressions, pattern matching, lists with map and filter and fold, tuples and variants of your own, records, options for missing answers, strings and formatted output, exceptions, and a printed report that uses all of it.

What You'll Learn

  • Print values with print_endline and tell int, float, string and bool apart
  • Name values with let, and give a name a short life with let ... in
  • Define functions, hand one to another, and write recursion with let rec
  • Use if/then/else as an expression, and combine conditions with && and ||
  • Choose between shapes with match, guards and the wildcard
  • Walk lists with List.map, filter, fold_left and sort
  • Group values as tuples and define variants with constructors of your own
  • Model a table with records, read a field, and copy one with a change
  • Answer a search that can fail with Some, None and Option.value
  • Slice and join strings, convert numbers, and format a line with Printf
  • Raise and catch exceptions, including the ones the library raises
  • Build a report from records, a sort, a grouping and formatted output

Prerequisites

  • No programming experience needed — the first chapter starts at the first line
  • If you know another language, the biggest change is that nothing here is a statement

Course Content

  1. 01
    Your first OCaml

    What OCaml is, how a script runs from the first line to the last, and the two ways to write a value out: print_endline and print_string.

    Beginner · 14 min read

    Not started
  2. 02
    Names, values and let

    Bindings with let, the difference between a name and a box, let ... in for a name with a short life, and the types the compiler works out for you.

    Beginner · 16 min read

    Not started
  3. 03
    Functions, and functions that call themselves

    Defining functions with let, handing one function to another, and the rec keyword that lets a function call itself on a smaller argument.

    Beginner · 18 min read

    Not started
  4. 04
    Decisions are expressions

    if/then/else answers a value rather than running a statement, why else is not optional, and how conditions are put together.

    Beginner · 14 min read

    Not started
  5. 05
    Pattern matching with match

    One form for choosing between shapes: literal patterns, a binding pattern, guards written with when, and the wildcard that catches the rest.

    Intermediate · 18 min read

    Not started
  6. 06
    Lists, and the functions that walk them

    A list is a chain built from :: and [], and map, filter, fold_left and sort replace almost every loop you would have written by hand.

    Intermediate · 20 min read

    Not started
  7. 07
    Tuples and shapes of your own

    Grouping values that belong together, taking a pair apart in a let, and a variant type that lists every shape a value may take.

    Intermediate · 20 min read

    Not started
  8. 08
    Records: data with names

    A record gives every field a name and a type, a copy with one field changed leaves the old value alone, and a list of records is how a table is kept.

    Intermediate · 20 min read

    Not started
  9. 09
    When there is nothing to answer

    Some and None make a missing answer part of the type, and Option.value, Option.map and match are the three ways to use one.

    Advanced · 18 min read

    Not started
  10. 10
    Text, and lines of formatted output

    Joining, slicing and measuring strings, turning numbers into text and back, and Printf.printf with the s, d and f conversions.

    Advanced · 20 min read

    Not started
  11. 11
    Exceptions, and catching them

    raise stops the current computation with a value, try ... with catches it, and the standard library raises a few exceptions of its own.

    Advanced · 18 min read

    Not started
  12. 12
    A report from records, sorting and grouping

    One program that uses records, a sort with a comparison, a fold for totals and Printf for a report: four books, their pages, and a total per genre.

    Pro · 26 min read

    Not started