|
|
|
|
|
|
|
|
|
|
|
|
|
|
Using recursion in LotusScript (continued)
The elements of a recursive function You can see how recursive functions differ from ordinary functions. A recursive function simply breaks down a larger problem into smaller problems until you arrive at a simple answer. Once that simple answer has been attained, the recursive function works its way backward to figure out the ultimate answer.
Let's look at one of those lines of code again:
This is what I call the end case (sometimes referred to as the base case). The end case is the end of the line for a recursive function. The problem is now so simple that it can be directly solved. All recursive functions have an end case.
Let's look at the other relevant line of code:
Factorial = i * Factorial(i - 1)
|
This is what's known as the recursive case. If the problem's too difficult to solve, we break it down into a simpler problem by making a recursive call. All recursive functions have a recursive case.
Now that we know the elements of a recursive function, let's put it all together. A recursive function asks a simple question -- can the problem at hand be solved directly? If so, we apply the end case. If not, we need to break the problem down further, so we apply the recursive case.
While writing a recursive function, I often have to remind myself of the simplicity of the concept. It can be easy to get so bogged down in the details that you find yourself reverting back to writing code in a non-recursive manner. In this case, calculating factorials is fairly easy. Almost all recursive functions are more difficult. Next, we'll look at an example that's slightly more difficult, but much more useful.
Duplicating @ReplaceSubstring in LotusScript While knowing how to calculate a factorial may help you on Jeopardy!, it doesn't come in handy too often when designing applications. Let's try writing something we can use.
Like many Domino developers, I often rewrite formula language functions in LotusScript, both for ease of use and to tweak the functionality to my liking. The @ReplaceSubstring function is a perfect candidate for translation into recursive LotusScript because we can easily think of it in a recursive manner.
Let's say you want to replace all of the back slashes in a string with forward slashes (something that happens quite often when dealing with URLs). If you were to do this manually, you'd probably use a simple method: search for a back slash, and if you found one, you'd replace it with a forward slash and continue your search (the recursive case). If there were no more back slashes, you'd know the string was fine the way it stood (the end case).
Let's dive right in to some code:
Function ReplaceSubstring(fullstring As String, replace As String, replacewith As String) As String
Dim lefttext As String
Dim righttext As String
Dim fulltext As String
If Not Instr(fullstring, replace) = 0 Then
lefttext = Left(fullstring, Instr(fullstring, replace)-1)
righttext = Right(fullstring, Len(fullstring) - (Instr(fullstring, replace) - 1 +_
Len(replace)))
fulltext = lefttext + replacewith + righttext
ReplaceSubstring = ReplaceSubstring(fulltext, replace, replacewith)
Else
ReplaceSubstring = fullstring
End If
End Function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Advertisement --
Learn Notes and Domino 8 at your place and pace!
Learn Notes and Domino in your office and/or home! TLCC's highly acclaimed distance learning courses for users, developers, and admins will enhance your career and your resume.
The many included activities and demos will make you a pro! Expert instructor help is a click away.
Click here to try a FREE demo course!! |
-- Advertisement --
Struggling with exporting Notes data to spreadsheets? No More!
Try IntelliPRINT, The world's leading Reporting, Dashboards, and Analysis solution for Notes & Domino
- Don't spend unproductive time maintaining different versions of the same spreadsheet
- Preserve data integrity and security in multi-user environments
- Create reports in minutes INSIDE Notes
- Get freedom from iterative report requests, deliver self-serve capabilities
Experience Reporting, Dashboards, and Analysis INSIDE Notes.
Try IntelliPRINT NOW! |
|
|
|
|
|
|
|
|
|
|