From: Xavier Leroy <Xavier.Leroy@inria.fr>
To: David.Bulone@ulp.u-strasbg.fr
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] Récursivité terminale
Date: Thu, 26 Mar 2009 15:59:32 +0100 [thread overview]
Message-ID: <49CB9854.1020707@inria.fr> (raw)
In-Reply-To: <20090326153839.8kzcsd6k0sg0csoc@webmail.u-strasbg.fr>
> Je voudrais savoir s'il existait un moyen de transformer une fonction
> récursive non terminale en fonction récursive terminale avec Caml.
[ Translation: is there a way to transform a non-tail-recursive function
into a tail-recursive function? ]
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).
For more complex data structures than lists, Huet's zippers can often
be used for the same purpose.
Happy Googling,
- Xavier Leroy
next prev parent reply other threads:[~2009-03-26 14:59 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 ` Xavier Leroy [this message]
2009-03-27 3:37 ` Chung-chieh Shan
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=49CB9854.1020707@inria.fr \
--to=xavier.leroy@inria.fr \
--cc=David.Bulone@ulp.u-strasbg.fr \
--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