34 lines
872 B
OCaml
34 lines
872 B
OCaml
let rec somme_liste l = match l with
|
|
| [] -> 0
|
|
| a::q -> a + somme_liste q;;
|
|
|
|
assert (somme_liste [1; 3; 2] = 6);;
|
|
|
|
let rec taille_liste l = match l with
|
|
| [] -> 0
|
|
| _::q -> 1 + taille_liste q;;
|
|
|
|
assert (taille_liste [1; 3; 2] = 3);;
|
|
|
|
let rec pow valeur exposant = match exposant with
|
|
| 0 -> 1
|
|
| i -> valeur * (pow valeur (i - 1));;
|
|
|
|
assert (pow 4 3 = 64);;
|
|
|
|
(* On peut remplacer i - 1 par exposant - 1 car les deux noms coexistent. *)
|
|
let rec part_ent_log2 nombre =
|
|
if nombre = 0 then failwith "Boum !";
|
|
(* Il est toujours utile de connaître les exceptions tôt. *)
|
|
if nombre = 1 then 0
|
|
else 1 + (part_ent_log2 (nombre / 2));;
|
|
|
|
assert (part_ent_log2 10 = 3);;
|
|
|
|
let rec est_dans_liste element l = match l with
|
|
| [] -> false
|
|
| a::q -> a = element || est_dans_liste element q;;
|
|
|
|
assert (est_dans_liste 2 [1; 3; 2]);;
|
|
assert (not (est_dans_liste 4 [1; 3; 2]));;
|