Re: stack overflow in tail recursive function

Daniel Fischer <[email protected]> Wed, 24 Mar 2010 15:03:43 +0100 (CET)
Newsgroups gmane.comp.lang.haskell.hugs.user
Message-ID <17751008.1022647.1269439423519.JavaMail.fmail@mwmweb053>
-----Urspr=C3=BCngliche Nachricht-----
Von: Bruno Schneider=20
Gesendet: 24.03.2010 13:56:32
An: [email protected]
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 buil=
ds 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 evaluat=
ed, 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 us=
e GHC and compile with optimisations,=20
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 ch=
eck that it does indeed.)=20

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

Cheers,
Daniel