Version française
Home     About     Download     Resources     Contact us    
Browse thread
to merge list of lists
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jon Harrop <jon@f...>
Subject: Re: [Caml-list] to merge list of lists
On Monday 05 March 2007 08:37, skaller wrote:
> On Mon, 2007-03-05 at 17:10 +1100, Pietro Abate wrote:
> > mergel [] [[1;2;3];[4;5;6];[7;8;9]];;
> > - : int list list = [[1; 4; 7]; [2; 5; 8]; [3; 6; 9]]
>
> In this case there is a library function:
>
>       List.concat
>
> that already does exactly what you want :)

List.concat doesn't do that:

# List.concat [[1;2;3];[4;5;6];[7;8;9]];;
- : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9]

Note that the OP is not asking for a concat or even a merge, but a transpose.

A naive (non tail recursive) transpose is a 1-liner:

# open List;;
# let rec transpose list =
    try map hd list :: transpose (map tl list) with _ -> [];;
val transpose : 'a list list -> 'a list list = <fun>

For example:

# transpose [[1;2;3];[4;5;6];[7;8;9]];;
- : int list list = [[1; 4; 7]; [2; 5; 8]; [3; 6; 9]]

-- 
Dr Jon D Harrop, Flying Frog Consultancy Ltd.
OCaml for Scientists
http://www.ffconsultancy.com/products/ocaml_for_scientists