Forum de discussion pour l'UE Programmation Fonctionnelle en OCaML

feuilles td 9 exercice 15

feuilles td 9 exercice 15

nosūtīja Hadrien Franco
Atbilžu skaits: 2

Bonjour, je cherche la correction pour l'exercice 15 de la feuille 9 de TD ocaml.

Merci d'avance.

Atbildot uz Hadrien Franco

Re: feuilles td 9 exercice 15

nosūtīja Irene Durand
let rec btree_fold e f btree = match btree with
Empty -> e
| Node(x, l, r) -> f x (btree_fold e f l) (btree_fold e f r)

let btree_size btree =
btree_fold 0 (fun x l r -> 1 + y + z) btree

let btree_sum btree =
btree_fold 0 (fun x l r -> x+y+z) btree

let btree_prod btree =
btree_fold 1 (fun x l r -> x*y*z) btree

let btree_height btree =
btree_fold (-1) (fun x l r -> if y > z then y + 1 else z + 1) btree

let btree_to_list btree =
btree_fold [] (fun x l r -> (x :: l) @ r) btree

(* equivalent to btree_mirror *)
let btree_twist btree =
btree_fold Empty (fun x l r -> Node(x, r, l)) btree

let btree_map f btree =
btree_fold Empty (fun x l r -> Node(f x, l, r)) btree