OCaml By Examples

values

utop

The integer type `int` can contain numbers from -4611686018427387904 to 4611686018427387903.
utop # 1 + 1;;
- : int = 2
The `float` type uses different symbols for operations: `+.`, `-.`, etc.
utop # 1.3 +. 2.5;;
- : float = 3.8
Bitwise operations can be performed using `lsl` (shift left), `lxor` (XOR), `land` (AND), etc.
utop # 1 lsl 3;;
- : int = 8
Comparing numbers can be done using `>`, `<`, and `<>` for inequality.
utop # 3 > 5;;
- : bool = false
Strings can be concatenated using the `^` operator.
utop # "hello" ^ " world";;
- : string = "hello world"
Variables can be declared via the `let` keyword.
utop # let a = 1;;
val a : int = 1
Functions are like variables, except that they take arguments specified after the name of the function and before the `=`. The last statement is automatically returned (no need to write `return`).
utop # let add_one i = i + 1;;
val add_one : int -> int = <fun>

utop # let a' = add_one a;;
val a' : int = 2
Operators can be overloaded and different types might define different operators.
utop # Int.( * ) 2 3;;
- : int = 6

utop # Int.( = ) 3 5;;
- : bool = false
If you see an operator you haven't seen before, you can check what it is on the [operator lookup](https://www.craigfe.io/operator-lookup/) page.
next: functions