From: Julien Verlaguet <julien.verlaguet@gmail.com>
To: Damien Doligez <damien.doligez@inria.fr>
Cc: OCaml List <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] tip for tail recursive map
Date: Mon, 2 Nov 2009 12:56:17 -0700 [thread overview]
Message-ID: <a01563690911021156g752f6f5fud3e6bb53eb922fd8@mail.gmail.com> (raw)
In-Reply-To: <8F7FB895-BC42-49C6-BAB3-4EDDF761C78B@inria.fr>
[-- Attachment #1: Type: text/plain, Size: 1475 bytes --]
Thanks for the tip !
I used this trick on every function of the List module and didn't try to go
a step further in specific cases.
Main problem being List.fold_right ... I couldn't figure out a way to encode
a more efficient continuation encoding
than the good old CPS, any ideas ?
Thanks
2009/11/2 Damien Doligez <damien.doligez@inria.fr>
>
> On 2009-10-23, at 21:55, pikatchou pokemon wrote:
>
> I know this topic has been discussed several times, but I don't think I
>> have seen the solution I use for functions of the List module which are not
>> tail recursive.
>> I thought sharing the tip could be nice.
>> I will take an example, List.map.
>> When rewritten in CPS map becomes:
>>
>> let rec map k f = function
>> | [] -> k []
>> | x :: rl -> map (fun res -> k ((f x) :: res)) f rl
>>
>
> You can do better with an ad-hoc encoding of the continuation
> instead of using closures:
>
>
> let rec map k f = function
> | [] -> List.rev k
> | x :: rl -> map (f x :: k) f rl
> ;;
>
> The memory footprint is smaller, and you spend much less time
> invoking closures.
>
> Note that I haven't bothered benchmarking these two functions.
>
> -- Damien
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
[-- Attachment #2: Type: text/html, Size: 2353 bytes --]
next prev parent reply other threads:[~2009-11-02 19:56 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-10-23 19:55 pikatchou pokemon
2009-11-02 10:33 ` [Caml-list] " Damien Doligez
2009-11-02 19:56 ` Julien Verlaguet [this message]
2009-11-02 20:04 ` Christophe Raffalli
2009-11-03 1:29 ` Yaron Minsky
2009-11-02 20:00 ` Christophe Raffalli
2009-11-02 23:30 ` Jon Harrop
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=a01563690911021156g752f6f5fud3e6bb53eb922fd8@mail.gmail.com \
--to=julien.verlaguet@gmail.com \
--cc=caml-list@yquem.inria.fr \
--cc=damien.doligez@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;
as well as URLs for NNTP newsgroup(s).