I am learning Haskell and I am getting an exception "stack overflow" I was not expecting.
The code is fairly simple:
type Reals = Double
prod :: Reals -> Reals -> Reals
prod a b = a*b
coprod :: Reals -> Reals -> Reals
coprod a b = a + b - (prod a b)
newsum :: Reals -> Reals -> Int -> Reals
newsum a b n =
approxA!!n
where
approxA = a : zipWith coprod approxA approxB
approxB = b : zipWith prod approxA approxB
The functions prod, coprod are intended to be evalated on reals in [0,1]. The function
newsum a b n
for n going to infinity, computes the function (a,b) -> min(1, a+b).
You can try
newsum 0.5 0.5 10
and
newsum 0.5 0.5 10000
to see this. However, by calling
newsum 0.5 0.5 10000000
I get a stack overflow. The newsum function is simple and makes a linear (in n) number of operations of Double type. In other words, the construction n-th element of the(infinite) lists of type [Reals]
approxA
and
approxB
takes linear time.
QUESTION 1: Why am I getting the stack overflow? | What are the limits of Haskell (or GHCi) and how can I estimate them (in advance) in a simple program like the one above?
QUESTION 2: Is there a flag/command in Haskell to instruct the interpreter to page the memory automatically, if needed? I want to write code that resemble Math, and don't want to spend time thinking about memory limits, stack overflows and stuff like that.
thanks!