
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

On Fri, 23 Apr 2021, Galaxy Being 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?
With, say transpose [] = [], the (zipWith (:)) would be shortened to the empty list. In contrast to that, (repeat []) provides as many list ends as needed.

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
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
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.

Think about it like this:
[image: image.png]
The current elements are zipped with the tails (which are created with
transpose). This has to bottom out with empty tails (repeat []).
On Sat, Apr 24, 2021 at 2:32 AM Galaxy Being
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
_______________________________________________ Haskell-Cafe mailing list To (un)subscribe, modify options or view archives go to: http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe Only members subscribed via the mailman list are allowed to post.
participants (4)
-
Galaxy Being
-
Henning Thielemann
-
Jeff Clites
-
Lyndon Maydwell