Version française
Home     About     Download     Resources     Contact us    
Browse thread
camlp4
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Olivier Andrieu <oandrieu@n...>
Subject: Re: [Caml-list] camlp4
On Jan 18, 2008 6:08 PM, Christian Sternagel
<Christian.Sternagel@uibk.ac.at> wrote:
> When using `camlp4o -parser Camlp4ListComprehension' as preprocessor,
> is the resulting code the naive translation, like in,
>
>  [(x, y) | x <- xs, even xs, y <- ys]
>
> =>
>
>  List.flatten (
>   List.map (fun x -> List.map (fun y -> (x, y)) ys) (List.filter even xs)
>  )
>
> or is there an optimization in order to avoid appends and minimize the
> number of cons?


FYI, my (old) syntax extension¹ for camlp4 <= 3.09 expands list
comprehensions to a combination of folds:

  [+ (x, y) | x <- xs | when even x | y <- ys]

=>

  List.fold_right
    (fun x __acc__ ->
       if even x then
         List.fold_right (fun y __acc__ -> (x, y) :: __acc__) ys __acc__
       else __acc__)
    xs []

Less cons operations, but it's not tail recursive.

[1] http://oandrieu.nerim.net/ocaml/#pa_compr
-- 
  Olivier