HP Forums
Tail Recursion, Bypass recursion limit - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Tail Recursion, Bypass recursion limit (/thread-19725.html)



Tail Recursion, Bypass recursion limit - FernandoMorea - 03-27-2023 08:47 PM

Factorial tail recursive:
1. create a fake function that calls the real function to not trigger the static analysis by the CAS that triggers the recursive evaluation to fail
2. save the function to be evaluated in a variable, so the user must re-run the result to grow the tail.
3. Enjoy.

Try to run this code, for example if you want to calculate 10!
myfact(10,1)

Code:

#cas
self_fact(a,b):= 
cont:= QUOTE(myfact(arg0,arg1));

myfact(x,curr):= 
IF x≤0
THEN curr
ELSE
arg0:=x-1; 
arg1:=x*curr; 
cont:= QUOTE(self_fact(arg0,arg1));
{'cont',arg1};
END;
#end