From: Chung-chieh Shan <ccshan@post.harvard.edu>
To: caml-list@inria.fr
Subject: Re: Récursivité terminale
Date: Thu, 26 Mar 2009 23:37:49 -0400 [thread overview]
Message-ID: <d9ut96-89i.ln1@mantle.rutgers.edu> (raw)
In-Reply-To: <49CB9854.1020707@inria.fr>
(So that's how you say "tail-recursive" in French...)
Xavier Leroy <Xavier.Leroy@inria.fr> wrote in article <49CB9854.1020707@inria.fr> in gmane.comp.lang.caml.inria:
> A technique that always works is to convert your function to
> continuation-passing style. The resulting code is hard to read and
> not particularly efficient, though.
>
> It is possible to do better in a number of specific cases. Functions
> operating over lists can often be made tail-rec by adding an
> "accumulator" parameter and reversing the accumulator at the end.
> For instance, List.map f l (not tail-rec) can be rewritten as
> List.rev (List.rev_map f l) (tail-rec).
In fact, it is always possible to do better in the sense above:
the accumulator parameter arises mechanically as the result of
defunctionalizing the continuation in a CPS program. For example, if we
transform "List.map f l (not tail-rec)" into CPS then defunctionalize
it, we get essentially "List.rev (List.rev_map f l) (tail-rec)".
Sometimes we can improve upon the first-order representation of
continuations chosen mechanically by defunctionalization. The factorial
function is an example: defunctionalization represents a continuation
by a list of numbers, but we can replace the list by the product of the
numbers.
--
Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig
100 Days to close Guantanamo and end torture http://100dayscampaign.org/
http://www.avaaz.org/en/end_the_war_on_terror/
prev parent reply other threads:[~2009-03-27 3:39 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-03-26 14:38 David.Bulone
2009-03-26 14:59 ` [Caml-list] " Xavier Leroy
2009-03-27 3:37 ` Chung-chieh Shan [this message]
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=d9ut96-89i.ln1@mantle.rutgers.edu \
--to=ccshan@post.harvard.edu \
--cc=caml-list@inria.fr \
/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