Basic Types, Values & Operators

OCaml has no implicit conversions and no operator overloading.

4.1 Primitive types

42, -7, 0xFF, 0o17, 0b1010, 1_000_000 63-bit on 64-bit platforms 3.14, 1., 1e10, nan, infinity IEEE 754 double true, false 'a', '\n', '\065', '\x41'

"hello", {|raw string|}, {foo|raw|foo} Immutable byte sequence "No meaningful value" int32, int64, nativeint 42l, 42L, 42n Boxed fixed-width integers

# max_int;;
- : int = 4611686018427387903
# Int64.max_int;;
- : int64 = 9223372036854775807L

4.2 Arithmetic — int and float operators are separate!

OCaml has no implicit conversions and no operator overloading.

abs_float / Float.abs

3 + 4          (* int addition *)
3.0 +. 4.5     (* float addition *)
10 / 3         (* = 3, integer division *)
10 mod 3       (* = 1 *)
10.0 /. 3.0    (* = 3.333... *)
2.0 ** 10.0    (* = 1024., float exponent *)
-5             (* unary minus; -. for floats: -.5.0 *)
float_of_int 3        (* 3. *)   (* also Float.of_int *)
int_of_float 3.99     (* 3  *)   (* truncation; also Float.to_int *)
int_of_string "42"    (* 42; raises Failure on bad input *)
int_of_string_opt "x" (* None *)
string_of_int 42      (* "42" *)
string_of_float 3.5   (* "3.5" *)
Float.round 2.5       (* 3. *)

4.3 Bitwise operators (int)

5 land 3   (* 1   AND *)
5 lor 3    (* 7   OR *)
5 lxor 3   (* 6   XOR *)
lnot 5     (* -6  NOT *)
1 lsl 4    (* 16  shift left *)
16 lsr 2   (* 4   logical shift right *)
-16 asr 2  (* -4  arithmetic shift right *)

4.4 Booleans & comparison

⚠️ Use = and <> for value equality. == and != compare memory addresses. Polymorphic compare raises on functions and may not terminate on cyclic values; for performance-critical code use typed comparisons (Int.equal, String.compare).

true && false        (* false *)
true || false        (* true *)
not true             (* false *)

1 = 1                (* structural equality *)
1 <> 2               (* structural inequality *)
"a" < "b"            (* polymorphic comparison works on any type *)
compare 3 5          (* -1 ; returns negative/0/positive *)
min 3 5, max 3 5

(* Physical (pointer) equality *)
let a = [1;2] and b = [1;2] in
(a = b, a == b)      (* (true, false) *)

4.5 Strings & chars at a glance

"Hello" ^ ", " ^ "World"     (* concatenation *)
String.length "abc"          (* 3 *)
"abc".[1]                    (* 'b' — indexing *)
String.uppercase_ascii "hi"  (* "HI" *)
Char.code 'A'                (* 65 *)
Char.chr 97                  (* 'a' *)

4.6 Unit

unit has exactly one value (). Functions called for side effects return unit:

print_string : string -> unit
print_endline "hi" ;;  (* - : unit = () *)

4.7 Operator precedence (highest → lowest, abbreviated)

!… ~… (prefix), . .( .[ #… (method/hash ops) function application, constructor application, assert, lazy - -. (unary) **… lsl lsr asr (right assoc)

*… /… %… mod land lor lxor (left) +… -… (left) @… ^… (right)

=… <… >… |… &… $… != (left) & && (right) or || (right) <- := (right) let match fun function try

Custom operators inherit precedence from their first character.