<div dir="ltr"><div>So it doesn't, but this one does (*):</div><div><br></div><div>---</div><div>transpose :: [[a]] -> [[a]]<br>transpose [] = []<br>transpose [r] = map (:[]) r<br>transpose (r1 : rrest) = zip' r1 (transpose rrest)<br>  where<br>    zip' [] bs = bs<br>    zip' (a : as) ~(b : bs) = (a : b) : zip' as bs<br><br>main = do<br>  mapM_ (print . transpose)<br>    [<br>      [[1,2,3]],<br>      [[1,2,3],[4,5,6]],<br>      [[1,2,3],[4,5,6],[7,8,9]],<br>      [[10,11],[20],[],[30,31,32]]<br>    ] <br>  print $ take 10 $ transpose [[0 ..]]<br>  --print $ map (take 10) $ transpose (repeat [0])<br>  print $ take 3 $ map (take 3) $ transpose (repeat [0..])</div><div>---</div><div><br></div><div>It's true that this implementation will make the commented line diverge.  <i>However</i>, (*) the stock implementation of transpose also diverges!  Try it: head <$> transpose (repeat [0]) in ghci will print [0 and then loop.  So I think this one is okay by that standard.  Does it do better at allocation than the existing one or the one with unzip?<br></div><div><br></div><div><rant, possibly beside the point><br></div><div>It's actually not even well-defined what the transpose would be if we allow infinitely many rows and also use the "condensing" property where the columns skip over nonexistent terms in the rows.  How would you even prove that transpose (repeat [0]) has length 1?  You'd have to rule out the existence of any second term in any of the infinitely many rows.  Of course, in this example there is a fast way to do that, but the rows could be anything, so the problem is uncomputable.  This logic is also why the final test above actually does terminate: because we find the requisite number of elements early on (in fact, instantly, since the rows are all infinite).</div><div><br></div><div>This actually raises the question of whether transpose even "should" care about infinitely many rows or columns (I know, the behavior is standardized; pretend we're discussing a new alternative transpose' that explicitly only does finite lists).</div><div></rant><br></div><div><br></div><div>Ryan<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, May 14, 2020 at 5:15 PM David Feuer <<a href="mailto:david.feuer@gmail.com">david.feuer@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto">That doesn't look like it works on infinite lists.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, May 14, 2020, 8:09 PM Ryan Reich <<a href="mailto:ryan.reich@gmail.com" target="_blank">ryan.reich@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>Hopefully I'm not fooling myself...how about this?</div><div><br></div><div>---</div><div>transpose :: [[a]] -> [[a]]<br>transpose [] = []<br>transpose (r1 : rrest) = zipWith' (:) (:[]) id r1 (transpose rrest)</div><div><br></div><div>zipWith' :: (a -> b -> c) -> (a -> c) -> (b -> c) -> ([a] -> [b] -> [c])<br>zipWith' _ _ fb [] bs = fb <$> bs<br>zipWith' _ fa _ as [] = fa <$> as<br>zipWith' f fa fb (a : as) (b : bs) = f a b : zipWith' f fa fb as bs<br><br>main = do<br>  mapM_ (print . transpose)<br>    [<br>      [[1,2,3]],<br>      [[1,2,3],[4,5,6]],<br>      [[1,2,3],[4,5,6],[7,8,9]],<br>      [[10,11],[20],[],[30,31,32]]<br>    ]</div><div>---</div><div><br></div><div>I see the output:</div><div>---</div><div>[[1],[2],[3]]<br>[[1,4],[2,5],[3,6]]<br>[[1,4,7],[2,5,8],[3,6,9]]<br>[[10,20,30],[11,31],[32]]</div><div>---</div><div><br></div><div>which is correct (the last example is the one from the haddocks).</div><div><br></div><div>I was concerned that the definition of zipWith' (which is akin to the Map function unionWith) is not compatible with the build/fold fusion rule, but the implementation of zipWith itself is basically the same.<br></div><div><br></div><div>Ryan<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, May 14, 2020 at 1:17 PM David Feuer <<a href="mailto:david.feuer@gmail.com" rel="noreferrer" target="_blank">david.feuer@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto">Right. I still think it might be the right thing to do, though. I'm not a big fan of general-purpose library functions that have any unnecessary memory leak hazard. The biggest counterargument is that real code is unlikely to run into that problem.</div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, May 14, 2020, 3:35 PM Ryan Reich <<a href="mailto:ryan.reich@gmail.com" rel="noreferrer" target="_blank">ryan.reich@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto"><div>My suggestion was much less sophisticated even than that, and is basically what David answered with fusion. Also according to his answer, the original code of transpose lacks the laziness that unzip's actual implementation would provide.</div><div dir="auto"><br></div><div dir="auto">I think what that means is that his concern over allocating extra pairs is about the ones created internally by unzip when it builds the lazy heads-and-tails accessors.<br><br><div class="gmail_quote" dir="auto"><div dir="ltr" class="gmail_attr">On Thu, May 14, 2020, 03:27 Andreas Abel <<a href="mailto:andreas.abel@ifi.lmu.de" rel="noreferrer noreferrer" target="_blank">andreas.abel@ifi.lmu.de</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 2020-05-13 22:27, Ryan Reich wrote:<br>
> Why not just inline the definition of unzip and hand-optimize away the <br>
> pairs?<br>
<br>
Isn't this what the original code of transpose is doing?<br>
<br>
> On Tue, May 12, 2020, 10:24 David Feuer <<a href="mailto:david.feuer@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">david.feuer@gmail.com</a> <br>
> <mailto:<a href="mailto:david.feuer@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">david.feuer@gmail.com</a>>> wrote:<br>
> <br>
>     Also, the more eager list allocation can increase residency, but I<br>
>     don't think it can cause a leak.<br>
> <br>
>     On Tue, May 12, 2020, 9:48 AM David Feuer <<a href="mailto:david.feuer@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">david.feuer@gmail.com</a><br>
>     <mailto:<a href="mailto:david.feuer@gmail.com" rel="noreferrer noreferrer noreferrer" target="_blank">david.feuer@gmail.com</a>>> wrote:<br>
> <br>
>         The cost of allocating the extra pairs.<br>
> <br>
>         On Tue, May 12, 2020, 5:11 AM Andreas Abel<br>
>         <<a href="mailto:andreas.abel@ifi.lmu.de" rel="noreferrer noreferrer noreferrer" target="_blank">andreas.abel@ifi.lmu.de</a> <mailto:<a href="mailto:andreas.abel@ifi.lmu.de" rel="noreferrer noreferrer noreferrer" target="_blank">andreas.abel@ifi.lmu.de</a>>> wrote:<br>
> <br>
>               > I don't know how much that'll cost in practice.<br>
> <br>
>             What costs are you worried about?<br>
> <br>
>             On 2020-05-12 00:08, David Feuer wrote:<br>
>              > In Data.List, we define<br>
>              ><br>
>              > transpose :: [[a]] -> [[a]]<br>
>              > transpose [] = []<br>
>              > transpose ([] : xss) = transpose xss<br>
>              > transpose ((x : xs) : xss) = (x : [h | (h : _) <- xss]) :<br>
>             transpose (xs<br>
>              > : [t | (_ : t) <- xss])<br>
>              ><br>
>              > The potential difficulty is that we essentially mapMaybe<br>
>             over the xss<br>
>              > list twice in the third case. So we hang on to the heads<br>
>             where we need<br>
>              > the tails and the tails where we need the heads. We could<br>
>             fix that with<br>
>              > something like this:<br>
>              ><br>
>              > transpose ((x : xs) : xss) = (x : fronts) : transpose (xs<br>
>             : rears)<br>
>              >    where<br>
>              >      (fronts, rears) = unzip [(h,t) | (h : t) <- xss]<br>
>              ><br>
>              > I don't know how much that'll cost in practice.<br>
>              ><br>
>              > _______________________________________________<br>
>              > Libraries mailing list<br>
>              > <a href="mailto:Libraries@haskell.org" rel="noreferrer noreferrer noreferrer" target="_blank">Libraries@haskell.org</a> <mailto:<a href="mailto:Libraries@haskell.org" rel="noreferrer noreferrer noreferrer" target="_blank">Libraries@haskell.org</a>><br>
>              > <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
>              ><br>
> <br>
>     _______________________________________________<br>
>     Libraries mailing list<br>
>     <a href="mailto:Libraries@haskell.org" rel="noreferrer noreferrer noreferrer" target="_blank">Libraries@haskell.org</a> <mailto:<a href="mailto:Libraries@haskell.org" rel="noreferrer noreferrer noreferrer" target="_blank">Libraries@haskell.org</a>><br>
>     <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
> <br>
> <br>
> _______________________________________________<br>
> Libraries mailing list<br>
> <a href="mailto:Libraries@haskell.org" rel="noreferrer noreferrer noreferrer" target="_blank">Libraries@haskell.org</a><br>
> <a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries" rel="noreferrer noreferrer noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries</a><br>
> <br>
</blockquote></div></div></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>
</blockquote></div>