<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">Hi Dan,<br><br>I wasn't aware of the third option, at least this particular variant of the *as* pattern. I've only seen it like this<br><br>&nbsp;<tt>f&nbsp;s@(x:xs)&nbsp; = ...<br><br></tt> i.e., outside the parens.<br><br>The cost question arose as I was deciding which way to write it.<br><br>Thanks,<br><br>Michael<br><tt></tt><br>--- On <b>Fri, 9/10/10, Dan Doel <i>&lt;dan.doel@gmail.com&gt;</i></b> wrote:<br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Dan Doel &lt;dan.doel@gmail.com&gt;<br>Subject: Re: [Haskell-cafe] Cost: (:) vs head<br>To: haskell-cafe@haskell.org<br>Cc: "michael rice" &lt;nowgate@yahoo.com&gt;<br>Date: Friday, September 10, 2010, 11:29 PM<br><br><div class="plainMail">On Friday 10 September 2010 11:13:50 pm michael rice wrote:<br>&gt; Which of these would be more costly for
 a long list?<br>&gt; <br>&gt; f :: [Int] -&gt; [Int]<br>&gt; f [x] = [x]<br>&gt; f (x:xs) = x + (head xs) : f xs<br>&gt; <br>&gt; f :: [Int] -&gt; [Int]<br>&gt; <br>&gt; f [x] = [x]<br>&gt; f (x:y:xs) = x + y : f (y:xs)<br><br>Another option would be:<br><br>&nbsp; f [x] = [x]<br>&nbsp; f (x:xs@(y:_)) = (x + y) : f xs<br><br>However, I believe I've done tests in the past, and your second example <br>generates the same code when optimizations are on (that is, it doesn't build a <br>new y:xs, but reuses the existing one), and that should perform the same as <br>your first implementation.<br><br>All that said, I'm not sure you'd be able to see the difference anyway.<br><br>-- Dan<br></div></blockquote></td></tr></table><br>