From: Jacques Garrigue <garrigue@math.nagoya-u.ac.jp>
To: twhitehe@uwo.ca
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Tail Recursion on Map, Append, etc.
Date: Thu, 17 Mar 2005 14:40:40 +0900 (JST) [thread overview]
Message-ID: <20050317.144040.107913221.garrigue@math.nagoya-u.ac.jp> (raw)
In-Reply-To: <200503161105.49726.twhitehe@uwo.ca>
From: Tyson Whitehead <twhitehe@uwo.ca>
> I was wondering about the status of map and friends with regard to tail
> recursion. There was a big discussion back in 2003 about specific solutions
> (implementation of the those functions using Obj) and more general compiler
> support for holes/destination passing. It started with the following
> message:
>
> http://caml.inria.fr/pub/ml-archives/caml-list/2003/01/4a9754e53ff07723caf21b4496d1d267.en.html
>
> It sounded to me like the general consensus was to immediately implement the
> specific tail recursive versions of these functions for List and friends
> (which were provided in the discusion), and then improve the compiler by
> adding support for advanced hole/destination passing solutions.
The result of this consensus is the extlib library. It provides those
tail-recursive functions.
http://ocaml-lib.sourceforge.net/
There is no project to include specific support in the compiler, but
the extlib implementation shows that this is not required if a bit of
magic is permitted.
Note that this list is not the core developers list, so the result of
discussions here does not imply anything on the ocaml distribution
itself.
> Looking at the list implementation in the OCaml Debian unstable source, it
> doesn't look like the more efficient version has been implemented. Further,
> looking at the assembler emitted for the code it doesn't look like the
> compiler supports holes/destination passing either.
To correct a misunderstanding: tail-recursive versions are not
necessarily more efficient (at least on short lists; short meaning
less than 10000 elements), but they are safer.
Non tail-recursive functions are carefully marked in the list module,
and alternative functions are included.
For instance:
let safe_map f l = List.rev (List.rev_map f l)
let safe_append l1 l2 = List.rev_append (List.rev l1) l2
provide you with tail recursive versions of map and append.
In practice they perform relatively well, if you don't want to
download extlib.
Jacques Garrigue
next prev parent reply other threads:[~2005-03-17 5:40 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-03-16 16:05 Tyson Whitehead
2005-03-16 17:06 ` [Caml-list] " Richard Jones
2005-03-17 5:40 ` Jacques Garrigue [this message]
2005-03-17 9:24 ` Christophe Raffalli
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=20050317.144040.107913221.garrigue@math.nagoya-u.ac.jp \
--to=garrigue@math.nagoya-u.ac.jp \
--cc=caml-list@yquem.inria.fr \
--cc=twhitehe@uwo.ca \
/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