Post Reply 
Tail Recursion, Bypass recursion limit
03-27-2023, 08:47 PM
Post: #1
Tail Recursion, Bypass recursion limit
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
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)