Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

This topic is 3920 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

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!

Posted (edited)

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
  • Like 1
Posted

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 ) )
Posted

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.

  • Like 1
Posted

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.

Posted

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? 

Posted

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.

Posted

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!

This topic is 3920 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

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