caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Christophe Raffalli <christophe.raffalli@univ-savoie.fr>
To: Julien Verlaguet <julien.verlaguet@gmail.com>
Cc: Damien Doligez <damien.doligez@inria.fr>,
	OCaml List <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] tip for tail recursive map
Date: Mon, 02 Nov 2009 21:04:37 +0100	[thread overview]
Message-ID: <4AEF3B55.2060604@univ-savoie.fr> (raw)
In-Reply-To: <a01563690911021156g752f6f5fud3e6bb53eb922fd8@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2752 bytes --]

Julien Verlaguet a écrit :
> 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 ?

reverse the list before and then to a fold_left ?

Cheers,
Christophe

> 
> Thanks
> 
> 2009/11/2 Damien Doligez <damien.doligez@inria.fr
> <mailto: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
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> 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

-- 
Christophe Raffalli
Universite de Savoie
Batiment Le Chablais, bureau 21
73376 Le Bourget-du-Lac Cedex

tel: (33) 4 79 75 81 03
fax: (33) 4 79 75 87 42
mail: Christophe.Raffalli@univ-savoie.fr
www: http://www.lama.univ-savoie.fr/~RAFFALLI
---------------------------------------------
IMPORTANT: this mail is signed using PGP/MIME
At least Enigmail/Mozilla, mutt or evolution
can check this signature. The public key is
stored on www.keyserver.net
---------------------------------------------


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 260 bytes --]

  reply	other threads:[~2009-11-02 20:04 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
2009-11-02 20:04     ` Christophe Raffalli [this message]
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=4AEF3B55.2060604@univ-savoie.fr \
    --to=christophe.raffalli@univ-savoie.fr \
    --cc=caml-list@yquem.inria.fr \
    --cc=damien.doligez@inria.fr \
    --cc=julien.verlaguet@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).