Skip to the content ForoHackers
Log inJoin

OCaml: start here

OCaml
#3079 · · Quote · 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

You are not logged in, so this goes up anonymously: no account, no name and no address is stored with it, and it may carry no link and no image. Log in or join to post under your name. Our web server's logs keep the address and time of every request for up to 14 days; to keep yours out of them, use Tor. To reach the staff in confidence, use the drop. Sign as a returning source (a key kept only in this browser)

Sending takes a few seconds here: your browser first works out a proof of work against spam.

Reply