List.fold_right
is not tail-recursive
as indicated here http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html
My question is why List.fold_right
was not implemented as tail-recursive
?
I think it is not hard to do so
let rev l =
let rec revr acc = function
| [] -> acc
| hd::tl -> revr (hd::acc) tl
in
revr [] l;;
let fold_right f l b =
let rev_l = rev l
in
let rec folder_rr acc = function
| [] -> acc
| hd::tl -> folder_rr (f hd acc) tl
in
folder_rr b rev_l;;
I also found in the library, a number of functions are not tail-recursive
while they can be implemented as tail-recursive
. How did the maker make such decisions?