Note that there is essentially no difference between f1 and f2.  When
you $! in f2, all it does is ensure that the argument isn't undefined.
It doesn't evaluate any of the list.  Try $!! from the DeepSeq module or
write your own list-forcing function.

For me, the following works:

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

f $!! x = force x `seq` f x
    where force [] = ()
          force (x:xs) = x `seq` force xs

whereas it doesn't without the $!!.



--
 Hal Daume III                                   | [EMAIL PROTECTED]
 "Arrest this man, he talks in maths."           | www.isi.edu/~hdaume


> -----Original Message-----
> From: [EMAIL PROTECTED] 
> [mailto:[EMAIL PROTECTED] On Behalf Of Koji Nakahara
> Sent: Wednesday, June 18, 2003 5:18 PM
> To: [EMAIL PROTECTED]
> Subject: Help: Stack-overflow and tail-recursive functions
> 
> 
> 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?
> 
> _______________________________________________
> Haskell-Cafe mailing list
> [EMAIL PROTECTED] 
> http://www.haskell.org/mailman/listinfo/haskel> l-cafe
> 
_______________________________________________
Haskell-Cafe mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to