Jono
Jono

Reputation: 3633

List of List turn 90 degrees in haskell

Say I have a list of lists of a type: can I rotate it (in a sense) so that:

[[a,b],    [[b,d],
 [c,d]] =>  [a,c]]

For any size list? Or if not possible for arbitrary sized list just for a list of size 6x6

Upvotes: 2

Views: 507

Answers (2)

Mikhail Glushenkov
Mikhail Glushenkov

Reputation: 15078

The following should give you the desired output:

import Data.List (transpose)

rotate :: [[a]] -> [[a]]
rotate = reverse . transpose

Testing:

*Main> rotate [[1,2],[3,4]]
[[2,4],[1,3]]
*Main> rotate [[1,2,3],[4,5,6],[7,8,9]]
[[3,6,9],[2,5,8],[1,4,7]]

Upvotes: 9

Daniel Wagner
Daniel Wagner

Reputation: 153182

Just Hoogle it!

[[a]] -> [[a]]

Upvotes: 4

Related Questions