
Hi Dan,
I wasn't aware of the third option, at least this particular variant of the *as* pattern. I've only seen it like this
f s@(x:xs) = ...
i.e., outside the parens.
The cost question arose as I was deciding which way to write it.
Thanks,
Michael
--- On Fri, 9/10/10, Dan Doel
Which of these would be more costly for a long list?
f :: [Int] -> [Int] f [x] = [x] f (x:xs) = x + (head xs) : f xs
f :: [Int] -> [Int]
f [x] = [x] f (x:y:xs) = x + y : f (y:xs)
Another option would be: f [x] = [x] f (x:xs@(y:_)) = (x + y) : f xs However, I believe I've done tests in the past, and your second example generates the same code when optimizations are on (that is, it doesn't build a new y:xs, but reuses the existing one), and that should perform the same as your first implementation. All that said, I'm not sure you'd be able to see the difference anyway. -- Dan