[Haskell-beginners] foldl' vs seq

Stephen Tetley stephen.tetley at gmail.com
Mon Feb 1 08:15:16 EST 2010


I

On 1 February 2010 11:54, Gabi <bugspynet at gmail.com> wrote:
> Hi Stephen,
> Thanks for the answer. Now I got confused :)
>
> 1. What is seq used for if it doesn't force strictness ?
> 2. Why the accumulator is important ? In other words why the following
> is much slower ? Isn't it RT too ?
>
> slowSum :: [Integer] -> Integer
> slowSum[] = 0
> slowSum (x:xs) = x `seq` x + slowSum xs

Hi Gabi

It seems to be classified as 'almost tail recursion', see here:

http://www.haskell.org/haskellwiki/Performance/Accumulating_parameter

In slowSum although x gets 'forced' it still has to be added to the
result of the recursive call [ slowSum xs ], so there isn't much that
can actually be done with it at each recursive step until you get an
answer back (what it actually does is build up the notorious [x +  ..]
thunks).

Best wishes

Stephen


More information about the Beginners mailing list