let apply =
fun f x ->
f x;;
let is_empty =
fun x ->
match x with
| [] -> true
| x :: y -> false;;
let rec length =
fun x ->
match x with
| [] -> 0
| x :: y -> (length y) + 1;;
let rec sum_of_list =
fun list ->
match list with
| [] -> 0
| first :: rest -> first + (sum_of_list rest);;
let rec map =
fun f list ->
match list with
| [] -> []
| first :: rest -> (f first) :: (map f rest);;
let left =
fun pair ->
match pair with
| a, _ -> a;; (* Le motif "_" est dit «irréfutable» *)
let right =
fun pair ->
match pair with
| _, b -> b;;
let right_implemented_with_let =
fun pair ->
let a, b = pair in
b;;
let head =
fun list ->
match list with
| h :: _ -> h
| [] -> 42;;
let rec exists =
fun predicate list ->
match list with
| [] -> false
| first :: rest ->
if predicate first then
true
else
exists predicate rest;;
let rec forall =
fun predicate list ->
match list with
| [] -> true
| first :: rest ->
if predicate first then
forall predicate rest
else
false;;
let rec length_with_abbreviated_syntax x =
match x with
| [] -> 0
| x :: y -> (length_with_abbreviated_syntax y) + 1;;
exception DivisionByZero;;
exception FailureContainingAnInt = int;;
raise DivisionByZero;; (* Similaire à "throw" en Java *)
try
123123
with
| DivisionByZero -> 2
| FailureContainingAnInt x -> x ;;
failwith "sdfsdfsdfsdf";; (* This will raise the exception "Failure" *)
let rec sum_of_float_list =
fun list ->
match list with
| [] -> 0.0
| first :: rest -> first +. (sum_of_float_list rest);;
let average list =
(sum_of_float_list list) /. (float_of_int (length list));;
let rec natural_square_root_starting_from n from =
let squared_from = from * from in
if squared_from = n then
from
else if squared_from < n then
natural_square_root_starting_from n (from + 1)
else (* squared_from > n *)
(from - 1);;
let natural_square_root n =
natural_square_root_starting_from n 0;;
type length = int;; (* type synonume *)
let rec even n =
if n = 0 then
true
else if n = 1 then
false
else
odd (n - 1)
and odd n =
if n = 0 then
false
else if n = 1 then
true
else
even (n - 1);;