[Hugs-users] stack overflow in tail recursive function

Daniel Fischer daniel.is.fischer at web.de
Wed Mar 24 10:03:43 EDT 2010


-----Ursprüngliche Nachricht-----
Von: Bruno Schneider 
Gesendet: 24.03.2010 13:56:32
An: hugs-users at haskell.org
Betreff: Re: [Hugs-users] stack overflow in tail recursive function

>On Wed, Mar 24, 2010 at 7:27 AM, Daniel Fischer wrote:
>[...]
>>
>> and this expression is only evaluated if necessary. factorial 20000 builds a thunk of 20000 nested multiplications, this is tried to evaluate when the value is demanded for printing, but the expression is too deeply nested to fit on the stack.
>>
>
>So it goes to the stack, hum? It thought it would be just a pointer to
>some computation type on the heap.


Well, the thunk is built on the heap, that's correct, but when it's evaluated, things go on the stack.

>
>Anyway, thanks for the detailed answer. I asked here because I didn't
>test that code on any other compiler/interpreter, so it could be
>something related to hugs implementation.
>

That's okay, but as a rule of thumb, if you don't know that it's
implementation-specific, haskell-cafe or beginners (depending on what
sort of answer you want) is the better choice; hugs-users or
glasgow-haskell-users are less frequented.

In this case, the behaviour is a little implementation-dependent, if you use GHC and compile with optimisations, 
the
strictness-analyser should see that the result of the multiplication is
needed and the compiler should make it strict by itself.
(I currently have no access to a computer with GHC installed, so I can't check that it does indeed.) 

>
>-- 
>Bruno Schneider
>http://www.dcc.ufla.br/~bruno/

Cheers,
Daniel


More information about the Hugs-Users mailing list