<div dir="ltr"><div><pre style="white-space:pre-wrap;color:rgb(0,0,0)">On Thu, Mar 16, 2023, 20:33 Todd Wilson <<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe">twilson at csufresno.edu</a>> wrote:
</pre>> ...</div><div><br></div>> <span style="color:rgb(0,0,0);white-space:pre-wrap">My question: can we do better than this? It seems that this solution is</span><pre style="white-space:pre-wrap;color:rgb(0,0,0)">> constantly building and breaking apart pairs. (Or is it, when optimized?)
</pre><br class="gmail-Apple-interchange-newline"><div>I don't think we can do better (as others have commented). Laziness is a benefit for peeling off only the beginning(s) of possibly-infinite (sub-)lists.</div><div><br></div><div>Possibly splitting off the head to call sub-function `run x xs` is also "breaking apart". If you don't like pairs, you can split the returned list itself to give the pairing.</div><div><br></div><div>Using `l@(y:zs)` as did Viktor might help a little if the compiler is particularly dumb. (GHC isn't.)</div><div><br></div><div>>    runs :: Ord a => [a] -> [[a]]<br>>    <br>>    runs   []  = []<br>>    runs l@[x] = [l]<br>>    runs (x: l@(y:zs))<br>>        | x <= y    = let (ys:zs) = runs l in ((x:ys) : zs)<br>>        | otherwise = ([x]: runs l)<br><br></div><div>This solution appears to  achieve the same level of laziness as Viktor's.  <br></div><div><br></div><div>AntC</div></div>