(Oh, Gabriel beat me to the punch, but I'll add my reply anyway.) The standard library is a fairly minimal library of basic features. Not a high-level full-featured library which helps you make good choices. :) However, the documentation (in .mli files) is fairly clear about what is tail-recursive or not. And there is rev_map: val rev_map : ('a -> 'b) -> 'a list -> 'b list (** [List.rev_map f l] gives the same result as {!List.rev}[ (]{!List.map}[ f l)], but is tail-recursive and more efficient. *) On Sun, Sep 28, 2014 at 1:31 PM, Shuai Wang wrote: > Hello list, > > > I am working on some stack_overflow exception in our recent project > written in OCaml > and eventually it turns out that this exception is thrown by List.map > function. > > By seeing the source code of OCaml's List module > , > it seems that map function > does not be implemented tail-recursively: > > let rec map f = function > [] -> [] > | a::l -> let r = f a in r :: map f l > > > > So my question is: > > *Why would OCaml's implementation List.map like this? * > > In my humble option, it definitely should be written in a tail-recursive > way, > and it not, stack_overflow would be unavoidable. > For example in order to handle the exception, > I abandon the code using List.map and rewrite it into a tail-recursive > help function. > > Best, > Shuai >