I'm new to haskell and would like some ideas on how to create a function that would take every head element of multilist's lists and add it to a new multilist, then move on to second column and etc.. So far I only know how to write a function for the first column: inverted (xss) = map head xss
How to make it to go until it does it with every column of a multilist?
CodePudding user response:
We can write this function directly, and you've got the exact right idea to start with. Get the heads first, and then make a recursive call.
Assuming all of the lists are of the same length,
inverted :: [[a]] -> [[a]]
inverted [] = [] -- Degenerate case
inverted xss
| null (head xss) = []
| otherwise = map head xss : inverted (map tail xss)
As it happens, this function is already in the standard library: it's called Data.List.transpose
and it does exactly what you want.