Skip to the content ForoHackers
Log inJoin

OCaml: start here

OCaml
#573 · Ophuscado · · Report

OCaml is a statically typed functional language that also allows imperative and object-oriented code. It has strong type inference, fast native compilation and a pragmatic approach: strict evaluation and mutation when you need them. It is a favourite for writing compilers, theorem provers and static analysers, and for systems where types should catch mistakes.

Key concepts

  • Type inference: types are checked at compile time without being written everywhere.
  • Variants and pattern matching: data that is one of several cases, handled with exhaustiveness checks.
  • Modules and functors: a rich module system that groups code and parameterises modules by other modules.
  • Immutable by default, mutation allowed: records and lists are immutable; references and arrays are mutable.
  • Strict evaluation: arguments are evaluated before a call, unlike lazy languages.
  • Options and results: absence and failure are values, with exceptions kept for exceptional cases.

Vocabulary

  • Variant: a type with several named constructors, also called a sum type.
  • Functor: in OCaml, a module-level function from modules to modules.
  • Dune and opam: the build tool and the package manager.
  • Toplevel: the interactive prompt for trying code.

What to study, in order

  1. Expressions, let bindings, functions and recursion in the toplevel.
  2. Lists, tuples, records, variants and pattern matching.
  3. Higher-order functions, Option and Result, and exceptions.
  4. Modules and signatures; split a program into parts with interfaces.
  5. A project with a build tool: an interpreter for a small language with a parser and an evaluator.
  6. Functors and the standard library's data structures.

Related tags

#functional-programming #haskell #compilers #type-theory

Log in or join to reply.