English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
optimization of sequence of List.map and inlining
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2008-06-10 (19:25)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] optimization of sequence of List.map and inlining
On Tuesday 10 June 2008 20:01:12 Charles Hymans wrote:
> Let's say, I have the following code:
>   let f l = List.map succ l
>   ....
>   let l = f l in
>   let l = List.map succ l in
>     do_something_with l
> Is there a way to tell the compiler to optimize it so that it runs as fast
> as this code:
>   let l = List.map (fun x -> succ (succ x)) l in
>     l
> In the first case, there are two passes where succ is applied to each
> elements of the list.
> In the second case, there is only one pass that applies succ twice to each
> element of the list.

In MLs, you deforest by hand. You might like to use a function composition 
operator like << from F#:

  let lst = map (f << f) lst

Dr Jon D Harrop, Flying Frog Consultancy Ltd.