caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* to merge list of lists
@ 2007-03-05  6:10 Pietro Abate
  2007-03-05  8:37 ` [Caml-list] " skaller
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Pietro Abate @ 2007-03-05  6:10 UTC (permalink / raw)
  To: ocaml ml

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]]

I've written it down, but to me, it looks overly complicated :

let rec mergel acc ll =
    let rec aux (al,all) = function
        [] -> (List.rev al,List.rev all)
      | [] :: tl -> aux (al,all) tl
      | (h :: l) :: tl -> aux ((h::al),(l::all)) tl
    in match aux ([],[]) ll with
      |([],[]) -> List.rev acc
      |(l,[]) -> l::acc
      |(l,tl) -> mergel (l::acc) tl
;;

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,
...). (?)

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 ?

Any hints ?

:)
p

-- 
++ Blog: http://blog.rsise.anu.edu.au/?q=pietro
++ 
++ "All great truths begin as blasphemies." -George Bernard Shaw
++ Please avoid sending me Word or PowerPoint attachments.
   See http://www.fsf.org/philosophy/no-word-attachments.html


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2007-03-07 14:33 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-05  6:10 to merge list of lists 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
2007-03-05 14:42   ` Zheng Li
2007-03-06  0:07 ` [Caml-list] " Pal-Kristian Engstad

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).