Transpose (Haskell)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Haskell | Python

[edit] Recursion

<<transpose_recursive.hs>>=
transpose :: [[a]] -> [[a]]
transpose ([]:_) = []
transpose mss = (map head mss) : transpose (map tail mss)

[edit] List Comprehension

<<transpose_list_comprehension.hs>>=
transpose :: [[a]] -> [[a]]
transpose mss = [ map (!!i) mss | i <- [0..length (mss !! 0) - 1]] 

[edit] Higher Order

<<transpose_higher_order.hs>>=
transpose :: [[a]] -> [[a]]
transpose mss = map (\i -> map (!!i) mss) [0..length (mss!!0) - 1]
Download code
hijacker
hijacker
hijacker
hijacker