[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: New to the group...

From: Ludovic Courtès
Subject: Re: New to the group...
Date: Thu, 04 May 2006 15:08:51 +0200
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.4 (gnu/linux)


"Jason Meade" <address@hidden> writes:

> (define fact
>  (lambda (n)
>    (cond
>     ((= n 1) 1)
>     (else
>      (* n (fact (- n 1)))))))
> guile> (fact 69)
> 171122452428141311372468338881272839092270544893520369393648040923257279754140647424000000000000000
> guile> (fact 70)
> ERROR: Stack overflow
> ABORT: (stack-overflow)

Your definition of `fact' is not tail-recursive, i.e., for each
recursive call, a new stack frame is created.  In order to remove this
problem, you have to rewrite `fact' so that it is tail-recursive (which
means that the function returns immediately after the recursive call):
this way Guile will not create any new stack frame for the recursive

See for instance the definition of `tail-factorial' there: .


reply via email to

[Prev in Thread] Current Thread [Next in Thread]