[Haskell-beginners] Restoring interleaved lists?

Felipe Lessa felipe.lessa at gmail.com
Thu Aug 12 21:23:13 EDT 2010


On Thu, Aug 12, 2010 at 10:11 PM, Patrick LeBoutillier
<patrick.leboutillier at gmail.com> 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 =).

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.


More information about the Beginners mailing list