11 Replies Latest reply on Feb 21, 2016 7:37 AM by beverly

# Tail recursion equivalent to this...?

Hi everyone,

In order for me to understand tail recursion with a sample as simple as possible would anybody put a tail recursion version of the following funcion?

Stacked version of Reverse ( Text ):

Case (

Length ( Text ) > 1 ;

Reverse ( Right ( Text ; Length ( Text ) - 1 )  ) & Left ( Text ; 1 ) ;

Text

)

Thank you very much in advance.

Kind regards,

Miguel

• ###### 1. Re: Tail recursion equivalent to this...?

FileMaker Custom Function:Reverse ( Alpha )

or this:

FileMaker Custom Function:Reverse ( Text )

tail - recursion calls itself until there is nothing to call, then returns the full result(s)

See if these help:

Understanding a tail recursive FileMaker Custom Function.

FileMaker Custom Functions | Browse Functions

beverly

• ###### 2. Re: Tail recursion equivalent to this...?

Thank you very much Beverly.

The first one is what I wanted.

The second one is not tail recursion AFAIK.

• ###### 3. Re: Tail recursion equivalent to this...?

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.

• ###### 4. Re: Tail recursion equivalent to this...?

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.

• ###### 5. Re: Tail recursion equivalent to this...?

you can create a CF that calls Reverse ( "Hello world" ; "" ) without the user knowing

• ###### 6. Re: Tail recursion equivalent to this...?

I thank you both very much.

Kind regards,

Miguel

• ###### 7. Re: Tail recursion equivalent to this...?

right! not necessarily tail recursion, but functions that may do what you want!

beverly

• ###### 8. Re: Tail recursion equivalent to this...?

I definitely agree!

Best regards,

Miguel

• ###### 9. Re: Tail recursion equivalent to this...?

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.

• ###### 10. Re: Tail recursion equivalent to this...?

Hi,

in the post marked as "Correct answer" there is a button that allows you unmark.