Why is the the transpose function in Data.List more complicated?

Transpose in Data.List is the following: transpose :: [[a]] -> [[a]] transpose [] = [] transpose ([] : xss) = transpose xss transpose ((x:xs) : xss) = (x : [h | (h:_) <- xss]) : transpose (xs : [ t | (_:t) <- xss]) Yet this seems to work. transp :: [[b]] -> [[b]] transp ([]:_) = [] transp rows = map head rows : transp (map tail rows) Why is the the transpose function in Data.List more complicated? -- -- Regards, KC

Hi KC,
transp :: [[b]] -> [[b]] transp ([]:_) = [] transp rows = map head rows : transp (map tail rows)
Why is the the transpose function in Data.List more complicated?
In the Data.List version, the list comprehension syntax quietly filters out items that fail to pattern-match (empty lists). Therefore the transpose in Data.List does not generate a pattern-match exception when you give it lists of different lengths: transpose [[1], [], [3]] == [[1,3]] The Data.List version also returns an empty list if the input is an empty list, whereas your version returns an infinite list of empty lists. -Greg
participants (2)
-
Greg Fitzgerald
-
KC