<div dir="ltr">Adding that case will require one to evaluate the second argument to check it's empty before allowing one to examine the result.<div><br></div><div>Consider `x ++ some_list_that_takes_a_long_time_to_produce_its_first_element`.</div><div><br></div><div>In this case your proposal will not be an optimisation. </div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Mar 4, 2018 at 8:32 PM, Ben Franksen <span dir="ltr"><<a href="mailto:ben.franksen@online.de" target="_blank">ben.franksen@online.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I found that in base [1] we have<br>
<br>
(++) [] ys = ys<br>
(++) (x:xs) ys = x : xs ++ ys<br>
<br>
I had expected there to be a clause<br>
<br>
(++) xs [] = xs<br>
<br>
at the top, which would avoid the list traversal in this common case.<br>
<br>
Is this somehow magically taken care of by the<br>
<br>
{-# RULES<br>
"++" [~1] forall xs ys. xs ++ ys = augment (\c n -> foldr c n xs) ys<br>
#-}<br>
<br>
? No, inlining @augment g xs = g (:) xs@ gives me<br>
<br>
(\c n -> foldr c n xs) (:) ys<br>
<br>
= foldr (:) ys xs<br>
<br>
which still traverses xs even if ys=[].<br>
<br>
Any thoughts?<br>
<br>
[1]<br>
<a href="http://hackage.haskell.org/package/base-4.10.1.0/docs/src/GHC.Base.html#%2B%2B" rel="noreferrer" target="_blank">http://hackage.haskell.org/<wbr>package/base-4.10.1.0/docs/<wbr>src/GHC.Base.html#%2B%2B</a><br>
<br>
Cheers<br>
Ben<br>
<br>
______________________________<wbr>_________________<br>
Haskell-Cafe mailing list<br>
To (un)subscribe, modify options or view archives go to:<br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-<wbr>bin/mailman/listinfo/haskell-<wbr>cafe</a><br>
Only members subscribed via the mailman list are allowed to post.</blockquote></div><br></div>