caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] Why List.map does not be implemented tail-recursively?
@ 2014-09-28 19:28 Shuai Wang
  2014-09-28 19:45 ` Malcolm Matalka
  0 siblings, 1 reply; 20+ messages in thread
From: Shuai Wang @ 2014-09-28 19:28 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 895 bytes --]

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
<https://code.ohloh.net/file?fid=P5Us_txNCMHIhpdfML6OZ8QN4Zs&cid=Jigg8RAfQdg&s=ocaml%20list.ml&pp=0&fp=305967&fe=ml&ff=1&filterChecked=true&mp=1&ml=1&me=1&md=1#L3>,
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

[-- Attachment #2: Type: text/html, Size: 6927 bytes --]

^ permalink raw reply	[flat|nested] 20+ messages in thread
* [Caml-list]  Why List.map does not be implemented tail-recursively?
@ 2014-09-28 19:31 Shuai Wang
  2014-09-28 19:36 ` Gabriel Scherer
  2014-09-28 19:45 ` Anthony Tavener
  0 siblings, 2 replies; 20+ messages in thread
From: Shuai Wang @ 2014-09-28 19:31 UTC (permalink / raw)
  To: caml-list

[-- Attachment #1: Type: text/plain, Size: 895 bytes --]

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
<https://code.ohloh.net/file?fid=P5Us_txNCMHIhpdfML6OZ8QN4Zs&cid=Jigg8RAfQdg&s=ocaml%20list.ml&pp=0&fp=305967&fe=ml&ff=1&filterChecked=true&mp=1&ml=1&me=1&md=1#L3>,
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

[-- Attachment #2: Type: text/html, Size: 7798 bytes --]

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2015-06-05 10:21 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-28 19:28 [Caml-list] Why List.map does not be implemented tail-recursively? Shuai Wang
2014-09-28 19:45 ` Malcolm Matalka
2014-09-28 20:26   ` Yaron Minsky
2014-09-29  2:31     ` Shuai Wang
2014-09-29  4:09       ` Anthony Tavener
2014-09-29  5:40         ` Martin Jambon
2014-09-29  9:13           ` Erkki Seppala
2014-09-29  9:15             ` Erkki Seppala
2014-09-28 19:31 Shuai Wang
2014-09-28 19:36 ` Gabriel Scherer
2014-09-28 19:45 ` Anthony Tavener
2014-09-29 12:08   ` Goswin von Brederlow
2014-09-29 14:02     ` Pierre Chambart
2014-09-29 15:44       ` Yaron Minsky
2014-09-29 21:00       ` Gabriel Scherer
2014-10-02 10:09         ` Stephen Dolan
2015-06-01 12:02           ` Jon Harrop
2015-06-02 12:04             ` Stephen Dolan
2015-06-05 10:21               ` Goswin von Brederlow
2014-09-30  6:29       ` Goswin von Brederlow

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).