What about this custom function:
tail - recursion calls itself until there is nothing to call, then returns the full result(s)
See if these help:
Thank you very much Beverly.
The first one is what I wanted.
The second one is not tail recursion AFAIK.
I think all functions, yours and both the functions suggested bij beverly are head-recursion. After 10000 iterations the result from any of them will be "?".
The following function has tail-recursion and will run not out of memory, (but after 50000 iterations FM stops calculating and give a "?" for result):
Let ( [
l = Length ( Text ) ;
c = Right ( Text ; 1 ) ;
Text = Left ( Text ; l - 1 )
Case ( l > 1 ; Reverse ( Text ; Result & c ) ; Result & c )
The difference between head- and tail-recursion is:
with head-recursion, the result of each iteration is concatenated to the first iteration, Like: Reverse ( "ABCD" ) & Reverse ( "ABC" ) & Reverse ( "AB" ) & "A" and result is returned at the end of all iterations. This consumes more memory each iteration.
with tail-recursion, the result is stored in a separate address in memory, which is overwritten in each iteration. Hence the always empty second "Result" parameter, which only serves as a placeholder for the result of the calc.
Thank you for your reply.
So the function would have to have two parameters eg. Reverse ( "Hello world" ; "" ), isn't it?
A bit confusing for the user I think.
I'll have to ponder it.
you can create a CF that calls Reverse ( "Hello world" ; "" ) without the user knowing
I thank you both very much.
right! not necessarily tail recursion, but functions that may do what you want!
I definitely agree!
You're welcome! Glad to have been of help!
Thank you for the credits , but I am surprised that a previously marked "solved" question can be re-activated and then be solved again, with the "correct answer" marking being assigned to another contribution. Having had that same situation, I didn't know that that it is possible.
in the post marked as "Correct answer" there is a button that allows you unmark.
Menno! your answer was worthy of a "correct".