OCaml By Examples

Language Basics

Project Management

  • dune
  • dune-release
  • opam pin

Advanced Ocaml

  • functors
  • monads

Libraries

Tuples

main.ml

Tuple hold fixed numbers of items of different types.
open Core

let () =
The items are separated by commas. Parenthesis are optional.
  let five = (5, "five") in
  let _sad = 5, "five" in (* Poor style and should be avoided *)
  let (_, second) = five in (* Extract component *)
  printf "Hey OCaml, give me %s!\n" second;
The first and last elements of a pair can be extracted with fst and snd.
  let first = five |> fst in (* Extract first component *)
  printf "I'll give you 3 + 2 = %d\n" first

console

Individual components are extracted by pattern matching syntax.
$ dune exec ./main.exe
Hey OCaml, give me five!
I'll give you 3 + 2 = 5
next: Lists