Il est fréquent que la récursion terminale soit imbriquée dans un if.
Le compilateur OCaml est-il capable de compiler ces deux fonctions:
D'une façon qui ne consomme pas plus de pile que ces deux fonctions:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 let rec list_max init l = match l with | [] -> init | a::l -> if a > init then list_max a l else list_max init l;; let rev_append_map_filter p f l1 l2 = let rec loop l acc = match l with | [] -> acc | h::t -> if p h then loop t (f h::acc) else loop t acc in loop l1 l2;;
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 let rec list_max init l = match l with | [] -> init | a::l -> list_max (if a > init then a else init) l;; let rev_append_map_filter p f l1 l2 = let rec loop l acc = match l with | [] -> acc | h::t -> loop t (if p h then f h::acc else acc) in loop l1 l2;;
Partager