TExcelTAD
Home › OCaml › OCaml Operators

OCaml Operators

Operators in OCaml are ordinary functions with symbolic names. Because the language never converts types implicitly, there are separate arithmetic operators for ints and floats.

Arithmetic

int float Meaning
+ +. addition
- -. subtraction
* *. multiplication
/ /. division (int division truncates)
mod Float.rem remainder
— ** exponentiation
~- or - prefix ~-. or -. prefix negation
ocaml · try it yourself
let () =
  Printf.printf "%d %d %d %d\n" (7 + 2) (7 - 2) (7 * 2) (7 / 2);
  Printf.printf "%d %d\n" (7 mod 2) (-7 mod 2);
  Printf.printf "%.1f %.1f %.2f %.1f\n" (7. +. 2.) (7. -. 2.) (7. /. 2.) (2. ** 3.);
  Printf.printf "%d %.1f\n" (- 5) (-. 2.5)
9 5 14 3
1 -1
9.0 5.0 3.50 8.0
-5 -2.5

-7 mod 2 is -1: the remainder takes the sign of the left operand, like in C.

Mixing the two families is a type error. 2 +. 3.0 fails because +. wants floats on both sides:

ocaml · try it yourself
let () = Printf.printf "%f\n" (2 +. 3.0)
File "<source>", line 1, characters 31-32:
1 | let () = Printf.printf "%f\n" (2 +. 3.0)
                                   ^
Error: The constant 2 has type int but an expression was expected of type
         float
Hint: Did you mean 2.?

Comparison

Comparison operators work on any type, as long as both sides have the same type. They compare structurally: two lists are equal when their elements are equal.

Operator Meaning
= <> structural equality / inequality
< <= > >= ordering
== != physical equality (same object in memory)
compare a b negative, zero or positive
ocaml · try it yourself
let () =
  Printf.printf "%b %b\n" (3 = 3) ("abc" <> "abd");
  Printf.printf "%b %b\n" ([1; 2] = [1; 2]) ((1, "a") < (1, "b"));
  Printf.printf "%d %d %d\n" (compare 1 2) (compare "b" "a") (compare 5 5);
  Printf.printf "%d %d\n" (min 4 9) (max 4 9)
true true
true true
-1 1 0
4 9

Warning: Use = and <> for equality. == and != check whether two values are the same object in memory. For example, "hi" == "hi" may be false, because they can be two different strings with equal contents. Beginners coming from C or JavaScript often reach for == by mistake.

Boolean operators

&& (and), || (or) and not work on bool. && and || are short-circuiting: the right side is evaluated only when needed.

ocaml · try it yourself
let safe_div a b = b <> 0 && a / b > 1

let () =
  Printf.printf "%b\n" (safe_div 10 2);
  Printf.printf "%b\n" (safe_div 10 0);
  Printf.printf "%b\n" (not (true || false))
true
false
false

Thanks to short-circuiting, safe_div 10 0 never evaluates 10 / 0, which would raise Division_by_zero.

Other common operators

Operator Types Meaning
^ string -> string -> string string concatenation
@ 'a list -> 'a list -> 'a list list concatenation
:: 'a -> 'a list -> 'a list add an element to the front of a list
|> 'a -> ('a -> 'b) -> 'b pipe a value into a function
@@ ('a -> 'b) -> 'a -> 'b apply a function (low precedence)
land lor lxor lsl lsr asr ints bitwise operations
! := refs read and write a reference
ocaml · try it yourself
let () =
  print_endline ("snake" ^ "_" ^ "case");
  Printf.printf "%d\n" (List.length ([1; 2] @ [3; 4; 5]));
  Printf.printf "%d %d %d\n" (12 land 10) (12 lor 10) (1 lsl 4);
  [3; 1; 2] |> List.sort compare |> List.iter print_int;
  print_newline ()
snake_case
5
8 14 16
123

Operators are functions

Wrap an operator in parentheses to use it as a regular function. You can even define your own:

ocaml · try it yourself
let ( +| ) (x1, y1) (x2, y2) = (x1 + x2, y1 + y2)

let () =
  Printf.printf "%d\n" (( + ) 3 4);
  Printf.printf "%d\n" (List.fold_left ( * ) 1 [1; 2; 3; 4; 5]);
  let x, y = (1, 2) +| (10, 20) in
  Printf.printf "(%d, %d)\n" x y
7
120
(11, 22)

Tip: Write ( * ) with spaces. (*) would start a comment!

Test yourself

What is 7 / 2 in OCaml?

  1. 3.5
  2. 3
  3. 4
  4. A type error

Which operator checks structural equality?

  1. ==
  2. ===
  3. =
  4. eq

How do you pass multiplication as a function to List.fold_left?

  1. (*)
  2. ( * )
  3. *
  4. mul