user3251180
user3251180

Reputation: 11

Reorder List of Lists in haskell

I have a list of lists. I need the elements inside reordered so that the new list of lists is a list of all the first elements, then a list of all the second elements, etc. It should look like this:

Input
[[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]]


Output
[[a1,b1,c1],[a2,b2,c2],[a3,b3,c3]]

Can anyone help me with this?

Upvotes: 1

Views: 478

Answers (2)

Titou
Titou

Reputation: 1008

In general when confronted with such issues :

Guess first result in your case ? transpose of course.

NB : You should accept answer from user3217013 - I wrote this because I can't edit yet

Upvotes: 4

Emil
Emil

Reputation: 2167

It looks like you want transpose, for which you need to import Data.List.

transpose will take the first elements of the lists that have them and put them in the first list in the right order, then the second elements of the lists that have them and put them in the second list in the right order, and so on.

For example,

> transpose [[1,2,3],[4,5],[6]]
[[1,4,6],[2,5],[3]]

> transpose [[1],[2,3],[4,5,6]]
[[1,2,4],[3,5],[6]]

Upvotes: 7

Related Questions