Skip to content

Instantly share code, notes, and snippets.

@joseanpg
Created October 27, 2013 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joseanpg/7179513 to your computer and use it in GitHub Desktop.
Save joseanpg/7179513 to your computer and use it in GitHub Desktop.
Matrix transposition usind foldl
import Control.Monad
transpose [] = []
transpose (z:zs) = foldl beta (alpha z) zs
where alpha = map (\x-> [x])
beta = zipWith (\x y-> x++[y])
transpose' [] = []
transpose' (z:zs) = map reverse $ foldl beta (alpha z) zs
where alpha = map (\x-> [x])
beta = zipWith (\x y-> y:x)
amatrix = [[11,12,13,14],[21,22,23,24],[31,32,33,34]]
main = do
print "original:";
mapM_ print amatrix
print "transpose:"
mapM_ print $ transpose amatrix
print "transpose':"
mapM_ print $ transpose' amatrix
{-
"original:"
[11,12,13,14]
[21,22,23,24]
[31,32,33,34]
"transpose:"
[11,21,31]
[12,22,32]
[13,23,33]
[14,24,34]
"transpose':"
[11,21,31]
[12,22,32]
[13,23,33]
[14,24,34]
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment