Very similar to the mathematical definition $$f : \mathbb{R} \to \mathbb {R},~ x\mapsto \exp(x) $$
let f (x:float) : float =
exp(x);;
f 4.3 ;;
let area_rectangle (width:int)(length:int):int =
width*length;;
area_rectangle 4 5;;
let area_rectangle_of_width_2 = area_rectangle 2;;
area_rectangle_of_width_2 5;;
Simpler example: to compute $n!=n(n-1)\ldots 1$, on may note that $$n! = n\times (n-1)! $$ and that $$1!=1$$ This is again a strictly decreasing sequence, it must converge!
let rec fact (n:int):int =
if (n=0 || n=1) then 1
else n * fact (n-1);;
(*
let rec fact (n:int):int =
if (n=0 || n=1) then (Printf.printf "Reached the end!";print_endline "";1)
else (Printf.printf "Now computing %d * (fact %d)\n" n (n-1);n * fact (n-1));;
*)
fact 5;;
An example with recursion: the function $\texttt{gcd}$ implements the observation that $$\texttt{gcd a b = gcd (a-b) b} $$ and so iteratively $$\texttt{gcd a b = gcd (a-2b) b} $$ $$\texttt{gcd a b = gcd (a-3b) b} $$ or equivalently in the end $$\texttt{gcd a b = gcd (a mod b) b} $$ so the numbers must $\textbf{strictly}$ decrease until reaching a point where $$\texttt{a mod b=0}$$
let rec gcd (a:int) (b:int) : int =
let r = a mod b in
if r = 0 then b
else gcd b r;;
(*
let rec gcd (a:int) (b:int) : int =
let r = a mod b in
if r = 0 then (Printf.printf "Found the gcd!";print_endline "";b)
else (Printf.printf "Now computing gcd %d %d\n" b r;print_endline "";gcd b r);;
*)
gcd 56 13;;
Here the output is $\textbf{itself}$ a function.
let affine (a:int) (b:int):int -> int =
fun x -> a*x+b ;;
let f = affine 3 4;;
f 4;;
Note that $\texttt{affine}$ has type "$\texttt{int -> int -> int -> int = <fun>}$". Where does it start, where does it end? In a way, everything in OCaml is functions:
let g = affine 3 ;;
g 4;;
g 4 5;;
Type $\texttt{int}$
1 + 3;;
1 + 2 + 3;;
-12;;
10/3;;
10 mod 3;;
lnot (-1);;
lnot 2;;
lnot 0b10;;
1 lsl 1;;
2 lsl 1;;
4 lsl 1;;
4 lsl 2;;
10 lsl 2;;
5 land 2;;
5 land 3;;
Type $\texttt{float}$
2. *. 3.14;;
10. /. 3.;;
float_of_int 10.;;
int_of_float 10.3;;
Type $\texttt{bool}$
true && false;;
true || false;;
not (true);;
true && (print_endline "coucou";false);;
false && (print_endline "coucou";true);;
difference between "$\texttt{=}$" and "$\texttt{==}$": beware of pointers! (same as in Python)
let a = [|0;1|];;
let b = a;;
let c = [|0;1|];;
(print_endline "b structurally equal to a?";b == a);;
(print_endline "c structurally equal to a?";c == a);;
(print_endline "c content equal to a?";c = a);;
a.(0) <- 2 ;;
b ;;
c ;;
Type $\texttt{char}$
Char.code 'a';;
Char.code 'A';;
Char.code 'b';;
Char.code 'B';;
Char.uppercase_ascii 'a';;
Char.lowercase_ascii 'a';;
Type $\texttt{unit}$
();;
();1;;
let a = (print_endline "Hello!";3+4);;
Even operators have a type!
(+);;
(<);;
Be careful of associativity!
8 / (4 / 2);;
(8 / 4) / 2;;
8 / 4 / 2 ;;
(print_endline "Test 1";false) || (print_endline "Test 2";true) || (print_endline "Test 3";false) ;;
if (1 > 2) then 'a' else 'b';;
if (true) then if false then 1 else 2 else 3;;
if (true) then print_endline "Ok!";;
if (true) then 2;;
if (false || true) then 'a' else 2;;