Created
October 27, 2013 09:16
-
-
Save joseanpg/7179513 to your computer and use it in GitHub Desktop.
Matrix transposition usind foldl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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