From: Gabriel Scherer <gabriel.scherer@gmail.com>
To: Shuai Wang <wangshuai901@gmail.com>
Cc: caml users <caml-list@inria.fr>
Subject: Re: [Caml-list] Why List.map does not be implemented tail-recursively?
Date: Sun, 28 Sep 2014 21:36:35 +0200 [thread overview]
Message-ID: <CAPFanBHYvq8t6sHH4CsaRofegxV4TiBKh=4YqMwvH-hewwaw=Q@mail.gmail.com> (raw)
In-Reply-To: <CAEQMQo=bZnk2+GRD1RpW_W-2GVrugVPgMCguwAYSYj7dqY3A_g@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 2356 bytes --]
The compiler library chose to keep it's implementation simple and clean, at
the cost of not being tail-recursive, and therefore unsuitable for large
lists. This is documented in the manual:
http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html
> Some functions are flagged as not tail-recursive. A tail-recursive
function uses constant stack space, while a non-tail-recursive function
uses stack space proportional to the length of its list argument, which can
be a problem with very long lists.
> List.map f [a1; ...; an] applies function f to a1, ..., an, and builds
the list [f a1; ...; f an] with the results returned by f. Not
tail-recursive.
Other libraries have made different design choices, so you can easily use a
different List module that provides tail-recursive operations. There are
several larger libraries, some (such as Batteries
http://batteries.forge.ocamlcore.org/ ) which directly extend the compiler
library, and are therefore usable as a drop-in replacement for it, some
others (such as Core
https://ocaml.janestreet.com/ocaml-core/111.28.00/doc/core/ ) which use
different conventions. They all provide tail-recursive mapping functions
suitable for use on long lists.
(Of course you can also simply replace `List.map f li` with `List.rev_map f
(List.rev li)` if you know `li` may be long.)
On Sun, Sep 28, 2014 at 9:31 PM, Shuai Wang <wangshuai901@gmail.com> 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
> <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: 9985 bytes --]
next prev parent reply other threads:[~2014-09-28 19:43 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-28 19:31 Shuai Wang
2014-09-28 19:36 ` Gabriel Scherer [this message]
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-09-30 8:46 ` [Caml-list] Why List.map does not be implemented oleg
2014-09-30 9:07 ` Gabriel Scherer
2014-10-01 10:29 ` oleg
2014-10-01 12:00 ` Gerd Stolpmann
2014-10-29 10:11 ` Gabriel Scherer
2014-10-02 10:09 ` [Caml-list] Why List.map does not be implemented tail-recursively? 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
-- strict thread matches above, loose matches on Subject: below --
2014-09-28 19:28 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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAPFanBHYvq8t6sHH4CsaRofegxV4TiBKh=4YqMwvH-hewwaw=Q@mail.gmail.com' \
--to=gabriel.scherer@gmail.com \
--cc=caml-list@inria.fr \
--cc=wangshuai901@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).