Find all first, then all second, then all third etc. elements of lists in a list

Viewed 546

Working with a self-made definition of a (9x9) matrix described as a list of lists of Maybe Ints. I want to create a function that returns the 9 columns of the matrix. I want to do this something like:

cols :: Matrix a -> [Block a]
cols matrix = map (!! n) matrix
 where
  n = (the values 1 .. 9)
  • Matrix is described as [Rows] or [[values]]

  • Block is described as [a]

So I want the output to be a list of lists, where those lists are a list of the first elements of the rows, the second elements of the rows, etc etc.

I see that

map (!! 1) matrix

will return a list of the second elements of the rows, ie the second column of the matrix; but I don't know how to extend this to all the columns of the matrix in a nice function.

2 Answers
Related