Restoring interleaved lists?

Hi all, I'm parsing a file that represents a type of musical notation (Guitar Pro file format). In the format there are t tracks, each composed of m measures. In the file the measures are all stored sequentially, i.e. from measure 1 to measure t*m, in the following fashion: - measure 1 of track 1 ; - measure 1 of track 2 ; - ... - measure 1 of track t ; - measure 2 of track 1 ; - measure 2 of track 2 ; - ... - measure 2 of track t ; - ... - measure m of track 1 ; - measure m of track 2 ; - ... - measure m of track t ; I need a function that, given t and the list of t*m measures, can spilt the measures by track, returning a list of t lists, each containing m measures. I cannot figure out how to do this, even though it seems to me like it shouldn't be too hard... I can't figure out how to "update" the lists of lists when I want to add a new element. Do anyone have any ideas? Thanks, Patrick -- ===================== Patrick LeBoutillier Rosemère, Québec, Canada

On Thu, Aug 12, 2010 at 10:11 PM, Patrick LeBoutillier
I need a function that, given t and the list of t*m measures, can spilt the measures by track, returning a list of t lists, each containing m measures. I cannot figure out how to do this, even though it seems to me like it shouldn't be too hard... I can't figure out how to "update" the lists of lists when I want to add a new element.
Do anyone have any ideas?
I won't post the complete code and will just throw ideas around, please let us know if you still get stuck =). We have the following function in the Prelude: splitAt :: Int -> [a] -> ([a], [a]) For example, Prelude> splitAt 3 [1..12] ([1,2,3],[4,5,6,7,8,9,10,11,12]) So you can write the following function (you can choose another name): separate :: Int -> [a] -> [[a]] For example, Prelude> separate 3 [1..12] [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] But that's not what we wanted. Hmmm, from Data.List we have transpose :: [[a]] -> [[a]] For example: Prelude Data.List> transpose [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] [[1,4,7,10],[2,5,8,11],[3,6,9,12]] Bingo! Hope that helps ;-), -- Felipe.

On Fri, 13 Aug 2010 03:23:13 +0200, Felipe Lessa
We have the following function in the Prelude:
splitAt :: Int -> [a] -> ([a], [a])
For example,
Prelude> splitAt 3 [1..12] ([1,2,3],[4,5,6,7,8,9,10,11,12])
So you can write the following function (you can choose another name):
separate :: Int -> [a] -> [[a]]
For example,
Prelude> separate 3 [1..12] [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
This is the splitEvery function of package split; you can find this with Hayoo, using the word "split". Regards, Henk-Jan van Tuyl -- http://Van.Tuyl.eu/ http://members.chello.nl/hjgtuyl/tourdemonad.html --

Felipe,
On Thu, Aug 12, 2010 at 9:23 PM, Felipe Lessa
On Thu, Aug 12, 2010 at 10:11 PM, Patrick LeBoutillier
wrote: I need a function that, given t and the list of t*m measures, can spilt the measures by track, returning a list of t lists, each containing m measures. I cannot figure out how to do this, even though it seems to me like it shouldn't be too hard... I can't figure out how to "update" the lists of lists when I want to add a new element.
Do anyone have any ideas?
I won't post the complete code and will just throw ideas around, please let us know if you still get stuck =).
Perfect!
We have the following function in the Prelude:
splitAt :: Int -> [a] -> ([a], [a])
For example,
Prelude> splitAt 3 [1..12] ([1,2,3],[4,5,6,7,8,9,10,11,12])
So you can write the following function (you can choose another name):
separate :: Int -> [a] -> [[a]]
For example,
Prelude> separate 3 [1..12] [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
But that's not what we wanted. Hmmm, from Data.List we have
transpose :: [[a]] -> [[a]]
For example:
Prelude Data.List> transpose [[1,2,3],[4,5,6],[7,8,9],[10,11,12]] [[1,4,7,10],[2,5,8,11],[3,6,9,12]]
Bingo!
Hope that helps ;-),
Yes, that's exactly what I was looking for! Thanks, Patrick
-- Felipe.
-- ===================== Patrick LeBoutillier Rosemère, Québec, Canada
participants (3)
-
Felipe Lessa
-
Henk-Jan van Tuyl
-
Patrick LeBoutillier