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=1.3 required=5.0 tests=SPF_FAIL 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 D350FBC69 for ; Mon, 5 Mar 2007 10:46:52 +0100 (CET) Received: from ciao.gmane.org (main.gmane.org [80.91.229.2]) by discorde.inria.fr (8.13.6/8.13.6) with ESMTP id l259kp1x013692 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NO) for ; Mon, 5 Mar 2007 10:46:52 +0100 Received: from list by ciao.gmane.org with local (Exim 4.43) id 1HO9mI-0006lS-K5 for caml-list@inria.fr; Mon, 05 Mar 2007 10:46:46 +0100 Received: from ivr94-8-88-162-26-239.fbx.proxad.net ([88.162.26.239]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 05 Mar 2007 10:46:46 +0100 Received: from li by ivr94-8-88-162-26-239.fbx.proxad.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Mon, 05 Mar 2007 10:46:46 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: caml-list@inria.fr From: Zheng Li Subject: Re: to merge list of lists Date: Mon, 05 Mar 2007 10:47:36 +0100 Message-ID: <87abysxbrb.fsf@pps.jussieu.fr> References: <20070305061050.GA21256@pulp.rsise.anu.edu.au> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: ivr94-8-88-162-26-239.fbx.proxad.net User-Agent: Gnus/5.110006 (No Gnus v0.6) Emacs/22.0.95 (gnu/linux) Cancel-Lock: sha1:KH3YahTX5cjZFdNL8JTCuIQUv5A= Sender: news X-Miltered: at discorde with ID 45EBE70C.000 by Joe's j-chkmail (http://j-chkmail . ensmp . fr)! X-Spam: no; 0.00; flatten:01 rec:01 rec:01 caml-list:01 writes:01 functions:01 pps:01 pps:01 jussieu:01 jussieu:01 int:01 primitives:01 caml:02 match:02 library:03 Pietro Abate writes: > Hi all, > I want to write a small function to merge a list of lists > > mergel [] [[1;2;3];[4;5;6];[7;8;9]];; > - : int list list = [[1; 4; 7]; [2; 5; 8]; [3; 6; 9]] > > Since my goal is to write it lazily, I'm wondering if there is a way of > re-write the same function just by using list primitives (map, flatten, > ...). (?) If you want to use high-order functions in the standard list library, here are my versions: open List let rec merge = function | [] -> [] | h :: [] -> map (fun x -> [x]) h | h::t -> map2 (fun x y -> x::y) h (merge t);; Note that, it's not efficient at all but quite easy to understand. Another version, let rec merge l = match rev l with | [] -> [] | h :: t -> fold_left (map2 (fun x y -> y::x)) (map (fun x -> [x]) h) t maybe better. > I always feel that when solving these kind of problems I miss some > greater truth ... for example, by using list comprehensions it's easy to > generalize a class of combinatorial problems. Is there a similar notion > I can use in this case ? A few weeks ago, someone else asked the permute question [1] in this list. There are instructive followups you may want to read. I'll also post my answer here sometime later. [1] http://caml.inria.fr/pub/ml-archives/caml-list/2007/02/cf0ae15f6f6e18ebf71c79c127d41a74.en.html -- Zheng Li http://www.pps.jussieu.fr/~li