Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

What is the (correct) general process to convert looping scripts to a recursive function?

Featured Replies

I'm having trouble getting my brain to work with the concept of recursive functions and how to build them.  (Although, that could be the allergy meds . . .)  :)
 
I have some script steps that loop through a date range and increment a counter if the day in the current iteration of the loop is a weekday.  If I'm grasping the process correctly, I should:

  1. Check to see if we've reached the end of recursion
  2. Do the increment of the counter
  3. If not the end, call the function and increment the parameter.

In my case, that should look something like this:

Case ( startDay = EndDay;
If (DayOfWeek (startDay) = 1 or DayOfWeek (startDay) = 7; counter; counter + 1);
myDateFunction (startDay + 1; endDay; counter)
)

However, this always returns a 0 or a 1.  How do I get the function to total up the number of weekdays?  I must be missing something completely obvious.  Any help would be appreciated!

You can try it this way:

Case ( 
startDay ≤ EndDay ;
myDateFunction ( startDay + 1 ; endDay ; counter + ( DayOfWeek ( startDay ) = 1 or DayOfWeek ( startDay ) = 7 ) ) ; 
counter )

---

BTW, I don't know of a general way to convert a looping script to a recursive function. Each case is a bit different. Keep in mind that a script can be either iterative or recursive, while a custom function is always recursive.

Edited by comment

Why use a counter and not simply carry the result over?

not ( DayOfWeek ( startDate ) = 1 or DayOfWeek ( startDate ) = 7 )
+ 
Case ( startDate < endDate ; MyDateFunction ( startDate + 1 ; endDate ) )

Why use a counter and not simply carry the result over?

 

Actually, it's good practice. Although it probably makes very little difference in this case, "carrying the result with you" instead of "leaving it on the ground" makes the function tail-recursive - thus increasing the number of allowable recursions from 10k to 50k.

Actually, it's good practice. Although it probably makes very little difference in this case, "carrying the result with you" instead of "leaving it on the ground" makes the function tail-recursive - thus increasing the number of allowable recursions from 10k to 50k.

 

Thanks, I'll keep that in mind.

  • Author

You can try it this way:

Case ( 
startDay ≤ EndDay ;
myDateFunction ( startDay + 1 ; endDay ; counter + ( DayOfWeek ( startDay ) = 1 or DayOfWeek ( startDay ) = 7 ) ) ; 
counter )

---

BTW, I don't know of a general way to convert a looping script to a recursive function. Each case is a bit different. Keep in mind that a script can be either iterative or recursive, while a custom function is always recursive.

 

Okay, I think I get it.  Use your "while" statement as the first argument to Case, then move forward and increment counter as you go, then return it when the "while" no longer holds true? 

Yes, but the main difference between this and what you had is passing the increased counter to the next recursion. Your original version increased the counter only at the very last call. That's why your result was either 0 or 1, depending (only) on endDay being a weekend or not.

  • Author

I think I was thinking that I had to build the function from the end (last recursion) and work backwards.  Didn't see how to carry a value forward through recursions.

 

Thanks for the help!

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.