[Haskell-cafe] repeat confusion

Henning Thielemann lemming at henning-thielemann.de
Fri Apr 23 16:37:57 UTC 2021


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.


More information about the Haskell-Cafe mailing list