Haskell: foldl' accumulator parameter

Posted by Clinton on Stack Overflow See other posts from Stack Overflow or by Clinton
Published on 2012-06-20T01:22:02Z Indexed on 2012/06/20 3:16 UTC
Read the original article Hit count: 143

Filed under:
|

I've been asking a few questions about strictness, but I think I've missed the mark before. Hopefully this is more precise.

Lets say we have:

n = 1000000
f z = foldl' (\(x1, x2) y -> (x1 + y, y - x2)) z [1..n]

Without changing f, what should I set

z = ...

So that f z does not overflow the stack? (i.e. runs in constant space regardless of the size of n)

Its okay if the answer requires GHC extensions.


My first thought is to define:

g (a1, a2) = (!a1, !a2)

and then

z = g (0, 0)

But I don't think g is valid Haskell.

© Stack Overflow or respective owner

Related posts about haskell

Related posts about lazy-evaluation