OCaml By Examples

imperative

utop

OCaml is a **functional** language, which is about evaluating functions. But programs are generally more complicated... One of the major feature of OCaml is the `in` keyword which allows to iteratively construct values that help evaluate the last expression.
utop # let one = 1 in
let two = one + 1 in
let four = two + two in
four + four;;
- : int = 8
But complex programs have side effects: functions that change something somewhere else, like printing to stdout or getting some input from stdin. These side-effects don't always return anything, but they can still be chained using the `in` keyword.
utop # let () = print_endline "hello" in
  let () = print_endline "world" in
  print_endline "!";;
hello
world
!
- : unit = ()
It is unatural though, and it is common to write in an **imperative** style by separating side-effects with a semicolon.
utop # print_endline "hello";
  print_endline "world";
  print_endline "!";;
hello
world
!
- : unit = ()
next: match