type nat = Zero | Succ of nat;;
let succ n =
Succ n;;
let pred n =
match n with
| Zero ->
failwith "predecessor of zero"
| Succ n_minus_1 ->
n_minus_1;;
let zero n =
match n with
| Zero -> true
| Succ _ -> false;;
let rec plus a b =
match a with
| Zero ->
b
| Succ(a_minus_1) ->
succ (plus a_minus_1 b);;
(* De int vers nat *)
let rec nat_of_int i =
match i with
| 0 -> Zero
| _ -> Succ(nat_of_int (i - 1));;
let rec int_of_nat n =
match n with
| Zero ->
0
| Succ n_minus_1 ->
1 + (int_of_nat n_minus_1);;
let rec times a b =
match a with
| Zero ->
Zero
| Succ a_minus_1 ->
plus b
(times a_minus_1 b);;
let f x y =
int_of_nat (times (nat_of_int x)
(nat_of_int y));;
type 'a tree =
| E
| N of 'a * ('a tree) * ('a tree);;
let rec height t =
match t with
| E ->
0
| N(_, t1, t2) ->
let height_of_t1 =
height t1
and height_of_t2 =
height t2
in
1 + (max height_of_t1 height_of_t2);;
let derive f =
let h = 0.00001 in
fun x ->
((f (x +. h)) -. (f x)) /. h;;