[Haskell-cafe] Cost: (:) vs head

Henning Thielemann schlepptop at henning-thielemann.de
Sun Sep 12 08:47:19 EDT 2010


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.


More information about the Haskell-Cafe mailing list