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: Pietro Abate <Pietro.Abate@a...>
Subject: to merge list of lists
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