Help: Stack-overflow and tail-recursive functions

Koji Nakahara yu-@div.club.ne.jp
Thu, 19 Jun 2003 09:18:18 +0900


Hi

I've written a function that is tail-recursive and takes
a list(queue) as one of its arguments.
However the function fails because of
"Stack space overflow"(GHC) or "ERROR - Garbage collection fails to reclaim sufficient space"(hugs).

For example (simplified), both of these results in stack-overflow:
f1 100000 [0,1,2], and even
f2 100000 [0,1,2],

where,
f1 0 l = l
f1 n l = f (n-1) $ take 3 $ l ++ [0,1]

f2 0 l = l
f2 n l = f2 (n - 1) $! (take 3 $ l ++ [0,1])

Why f2 overflows?

How to avoid this stack-overflow?