little recursion yields stack-overflow



Barton Willis schrieb:
> Using Maxima + GCL , it's too easy to run into such problems; a simple
> example:
> 
>  (%i1) f(k) := if k = 0 then 1 else f(k-1) + 1/k$
> 

However, it depends on the actual definition of the function:

(%i16) kill(all);
(%o0) done
(%i1) f(k) := if k = 0 then 1 else f(k-1) + 1/k$
(%i2) f(100);
(%o2) 
17255451288708850246802870400516623589983/2788815009188499086581352357412492142272
(%i3) f1(k, sum) := if k#0 then f1(k-1, 1/k + sum) else 1+sum$
(%i4) f1(100,0);
(%o4) 
17255451288708850246802870400516623589983/2788815009188499086581352357412492142272
(%i5) f(185);
Maxima encountered a Lisp error:
  Error in PROGN [or a callee]: Bind stack overflow.
Automatically continuing.
To reenable the Lisp debugger set *debugger-hook* to nil.
(%i6) f1(185,0);
(%o6) 
(529057799765496391378647435546833377236770851996580076368268346205236477711545943)/(77799508403016316788042253801219605514777819889683896088055584001023635421776000)


Actually, the compiled vesion of f outreaches the compiled version of f1 
(by far!).  BTW, Is there a way to look at the translate()d code?  Those 
.lsp files don't exist anymore after compilation.


(%i7) compile(f);
Compiling C:/DOKUME~1/martin/LOKALE~1/Temp/gazonk_7264_0.lsp.
End of Pass 1.
End of Pass 2.
OPTIMIZE levels: Safety=2, Space=3, Speed=3
Finished compiling C:/DOKUME~1/martin/LOKALE~1/Temp/gazonk_7264_0.lsp.
(%o7) [f]
(%i8) f(185);
(%o8) 
(529057799765496391378647435546833377236770851996580076368268346205236477711545943)/(77799508403016316788042253801219605514777819889683896088055584001023635421776000)
(%i9) f(1500);
(%o9) 877954585547499024136115143916[596 
digits]745016413528461400570658626577/987490003569313441331322369695[595 
digits]509757970780984941062302080000
(%i10) compile(f1);
Compiling C:/DOKUME~1/martin/LOKALE~1/Temp/gazonk_7264_0.lsp.
End of Pass 1.
End of Pass 2.
OPTIMIZE levels: Safety=2, Space=3, Speed=3
Finished compiling C:/DOKUME~1/martin/LOKALE~1/Temp/gazonk_7264_0.lsp.
(%o10) [f1]
(%i11) f1(1500,0);
Maxima encountered a Lisp error:
  Error in MACSYMA-TOP-LEVEL [or a callee]: Bind stack overflow.
Automatically continuing.
To reenable the Lisp debugger set *debugger-hook* to nil.


-- Martin