[Haskell-cafe] Rotating matrices

Stefan Holdermans stefan at cs.uu.nl
Thu Jun 15 09:31:07 EDT 2006


Thomas,

> > rotate' :: [[a]] -> [[a]]
> > rotate' [] = []
> > rotate' xs = (map (head) xs ):(rotate' $ filter (not . null) $  
> map (tail) xs)
>
> which seems to work just fine. While this solution is adequate (it  
> seems to work for infinite structures as well, which is good), I  
> originally set out to solve this problem with a fold or a mapAccum  
> of some sort (I don't really care if it doesn't work with infinite  
> structures). Can anyone suggest a way to write this with a fold (or  
> tell me why it isn't a good idea)?

   transpose = foldr (zipWith (:)) (repeat [])

HTH,

   Stefan


More information about the Haskell-Cafe mailing list