Jeff Clites jclites at mac.com
Fri Apr 23 16:57:52 UTC 2021

```With transpose, you get a pattern match failure if you try to transpose an empty list, and with transpose2 you get an infinite list (of empty lists). Those are in a sense two ways of representing failure, if you take an empty list to not be a valid matrix.

But the second definition “works” because zip (or zipWith) of two lists stops when either list runs out (so you can zip an infinite list with a finite one to get a finite result).

I guess the point is to observe that the “transpose [xs]” case is just a specialization of the “transpose (xs:xss)” if you choose “transpose []” carefully. I’m not sure if that’s deeply significant, though it’s a good thing to watch for (special cases that don’t need to be separate cases). In this situation it does make the failure case exotic though.

Jeff

> On Apr 23, 2021, at 9:31 AM, Galaxy Being <borgauf at gmail.com> wrote:
>
> I'm in Bird's Thinking Functionally with Haskell and he has this code to transpose a matrix based on a list of row lists
>
> transpose :: [[a]] -> [[a]]
> transpose [xs] = [[x] | x <- xs]
> transpose (xs:xss) = zipWith (:) xs (transpose xss)
>
> then he says transpose can be rewritten with this pattern
>
> transpose [] = ...
>
> what could be the rest of it? The answer he gives is
>
> transpose2 :: [[a]] -> [[a]]
> transpose2 [] = repeat []
> transpose2 (xs:xss) = zipWith (:) xs (transpose2 xss)
>
> where repeat [] gives an infinite list of repetitions. And, he says, note that
>
> transpose [xs] =  zipWith (;) xs (repeat []) = [[x] | x <- xs]
>
> I suppose I get this last equation, but I don't understand repeat in transpose2. Can someone explain this to me?
>
> LB
>
> _______________________________________________