[Haskell-beginners] Why is the the transpose function in Data.List more complicated?

Greg Fitzgerald garious at gmail.com
Sat Aug 4 02:05:10 CEST 2012


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



More information about the Beginners mailing list