caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Zheng Li <li@pps.jussieu.fr>
To: caml-list@inria.fr
Subject: Re: to merge list of lists
Date: Mon, 05 Mar 2007 10:47:36 +0100	[thread overview]
Message-ID: <87abysxbrb.fsf@pps.jussieu.fr> (raw)
In-Reply-To: <20070305061050.GA21256@pulp.rsise.anu.edu.au>

Pietro Abate <Pietro.Abate@anu.edu.au> 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


  parent reply	other threads:[~2007-03-05  9:46 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-03-05  6:10 Pietro Abate
2007-03-05  8:37 ` [Caml-list] " skaller
2007-03-05  8:53   ` Jon Harrop
2007-03-05 19:02     ` skaller
2007-03-05 19:40       ` skaller
2007-03-07 14:33     ` Roland Zumkeller
2007-03-05  9:47 ` Zheng Li [this message]
2007-03-05 14:42   ` Zheng Li
2007-03-06  0:07 ` [Caml-list] " Pal-Kristian Engstad

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87abysxbrb.fsf@pps.jussieu.fr \
    --to=li@pps.jussieu.fr \
    --cc=caml-list@inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).