[Haskell-beginners] A question on seq

Klaus Gy klausgy at gmail.com
Tue Sep 14 17:26:47 EDT 2010


Hi!

Inspred from the discussion
http://www.haskell.org/pipermail/beginners/2010-February/003396.html ,
I just try to understand the seq function a bit better. When I compare
these two functions:

firstSum :: Num a => a -> [a] -> a
firstSum s []     = s
firstSum s (x:xs) = seq help (firstSum help xs)
  where help      = x + s

secondSum :: Num a => [a] -> a
secondSum []      = 0
secondSum (x:xs)  = seq help (x + help)
  where help      = secondSum xs

What should be the difference? In my opinion both functions do not
return a complete unevaluated thunk (secondSum returns a thunk of the
form (THUNK + b) where THUNK contains a single numeral value). But it
seems to me that the first function computes the output somehow linear
in the sense that it does just a set of substitutions while the second
functions has to create a tree to handle all the recursive calls of
seq (sorry, my terminology is for sure a bit poor). So I would say the
first function delivers a better performance. (In the discussion I
mentioned, the second function was not discussed in this form with the
seq implementation). Am I right with my thoughts?

Thanks, fweth


More information about the Beginners mailing list