[Haskell-cafe] Cost: (:) vs head
michael rice
nowgate at yahoo.com
Sun Sep 12 09:10:27 EDT 2010
Hi Henning,
Thanks for the tip, I'll check it out.
A related but more general question: on average, what's more efficient, pattern matching or function calls?
Michael
--- On Sun, 9/12/10, Henning Thielemann <schlepptop at henning-thielemann.de> wrote:
From: Henning Thielemann <schlepptop at henning-thielemann.de>
Subject: Re: [Haskell-cafe] Cost: (:) vs head
To: "michael rice" <nowgate at yahoo.com>
Cc: haskell-cafe at haskell.org
Date: Sunday, September 12, 2010, 8:47 AM
michael rice schrieb:
> 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)
What about empty lists? How about
zipWith (+) xs (drop 1 xs ++ [0]) ?
Since I often need to combine adjacent list elements, I have defined
mapAdjacent in utility-ht.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100912/e3416c6d/attachment.html
More information about the Haskell-Cafe
mailing list