TExcelTAD
Home › OCaml › OCaml Syntax

OCaml Syntax

An OCaml source file (.ml) is a sequence of top-level definitions. Most of them start with let. There is no main function: the definitions are evaluated from top to bottom when the program starts.

ocaml · try it yourself
(* A comment looks like this. (* Comments can be nested. *) *)

let course = "OCaml"
let lessons = 44

let () =
  print_endline course;
  print_int lessons;
  print_newline ()
OCaml
44

Expressions, not statements

In many languages, code is a list of statements. In OCaml almost everything is an expression that produces a value: arithmetic, function calls, if, match, even a block of code. This means you can put an if wherever a value is expected:

ocaml · try it yourself
let temperature = 23

let () =
  let description = if temperature > 20 then "warm" else "cool" in
  print_endline ("It is " ^ description)
It is warm

The punctuation that matters

Symbol Meaning
let x = e a top-level definition
let x = e1 in e2 a local definition, visible only inside e2
e1; e2 evaluate e1 (which should be unit), then e2; the result is e2
;; ends a phrase in the toplevel; not needed in files
(* ... *) comment
f x y call f with arguments x and y: no parentheses or commas
( ... ) or begin ... end grouping

Function application is just juxtaposition. max 3 7 calls max with 3 and 7. You add parentheses only to group: print_int (max 3 7). Without them, print_int max 3 7 would try to pass three arguments to print_int.

ocaml · try it yourself
let () =
  print_int (max 3 7);
  print_newline ();
  print_endline (string_of_int (abs (-12)));
  print_endline (String.uppercase_ascii "shout")
7
12
SHOUT

Note: Negative literals need parentheses when passed as arguments: abs (-12). Writing abs -12 means abs - 12, a subtraction, which is a type error.

Sequencing with ;

The semicolon is a separator between expressions, not a terminator. The last expression in a sequence gives the value, so there is no ; after it. A trailing ; before in or at the end of a let () = body is a common beginner mistake that can confuse the parser.

ocaml · try it yourself
let () =
  print_string "one ";
  print_string "two ";
  print_endline "three"
one two three

Naming rules

  • Values and functions start with a lowercase letter or _: total, user_name, _unused.
  • Types are lowercase too: int, string, shape.
  • Constructors (variant tags) and module names start with an uppercase letter: Some, Circle, List, Printf.
  • Use snake_case. An apostrophe is allowed in names: x' is a common name for "the next x".
ocaml · try it yourself
let price = 12
let price' = price * 2
let _ignored = "no warning for unused names starting with _"

let () = Printf.printf "%d %d\n" price price'
12 24

Indentation

Indentation is not significant in OCaml, unlike Python. It is still important for humans; the standard formatter ocamlformat (run with dune fmt) keeps it consistent.

Test yourself

How do you call a function f with arguments 1 and 2?

  1. f(1, 2)
  2. f 1 2
  3. f[1][2]
  4. f(1)(2);

What does print_int abs -5 try to do?

  1. Print 5
  2. Print -5
  3. Something that fails to type-check, because -5 is parsed as subtraction
  4. Print the absolute value as a string

Which names must start with an uppercase letter?

  1. Functions
  2. Local variables
  3. Constructors and modules
  4. Types