From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on yquem.inria.fr X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=AWL autolearn=disabled version=3.1.3 X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from discorde.inria.fr (discorde.inria.fr [192.93.2.38]) by yquem.inria.fr (Postfix) with ESMTP id 4ABBFBC69 for ; Mon, 5 Mar 2007 09:59:15 +0100 (CET) Received: from pih-relay05.plus.net (pih-relay05.plus.net [212.159.14.132]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l258xEHj004466 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Mon, 5 Mar 2007 09:59:15 +0100 Received: from [80.229.56.224] (helo=beast.local) by pih-relay05.plus.net with esmtp (Exim) id 1HO92H-0003ya-QL for caml-list@yquem.inria.fr; Mon, 05 Mar 2007 08:59:14 +0000 From: Jon Harrop Organization: Flying Frog Consultancy Ltd. To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] to merge list of lists Date: Mon, 5 Mar 2007 08:53:11 +0000 User-Agent: KMail/1.9.5 References: <20070305061050.GA21256@pulp.rsise.anu.edu.au> <1173083859.25568.47.camel@rosella.wigram> In-Reply-To: <1173083859.25568.47.camel@rosella.wigram> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200703050853.12223.jon@ffconsultancy.com> X-Miltered: at discorde with ID 45EBDBE2.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; recursive:01 val:01 ocaml:01 ocaml:01 frog:98 wrote:01 wrote:01 rec:01 caml-list:01 tail:01 int:01 int:01 concat:03 concat:03 naive:03 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 = 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