I'm implementing transpose
in Haskell.
-- transpose [[1,2,3],[4,5,6],[7,8,9]]
-- [[1,4,7],[2,5,8],[3,6,9]]
Please give it a look. Note that I'm trying not to use any built-in functions (excluding map
and ++
):
transpose' :: [[a]] -> [[a]]
transpose' ys
| null $ filter (not . null) ys = []
| otherwise = [flatten' $ map head' ys] ++ transpose' (map tail' ys)
head' :: [a] -> [a]
head' [] = []
head' (x:xs) = [x]
tail' :: [a] -> [a]
tail' [] = []
tail' (x:xs) = xs
flatten' :: [[a]] -> [a]
flatten' as = foldl (\acc x -> acc ++ x) [] as
Side note - I know that I could look at the Haskell source, but I appreciate the comments & thoughtful insights here.