|
|
|
|
|
|
|
|
|
|
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
|
[ Prev | Next ]
|
|
|
|
|
|
-- Advertisement --
2-Minute Tutorials
How do I...
- integrate MS Office or OpenOffice with Notes?
- create cross-tab reports and charts?
- print serial letters and mailing labels?
- create PDFs in Lotus Notes?
Check out the 2-minute tutorials here. |
-- Advertisement --
How good are your Notes Reports?
Integra for Notes provides high value reporting and data analysis from Lotus Notes databases using Microsoft Word, Excel and PDF files.
- Enhance traditional static reports with Excel data analysis, pivot tables, macros
- Report from any Lotus Notes databases without changes to database design
- Runs reports through a Lotus Notes client and a web browser
- Enables Report scheduling or distribution by e-mail, printing or storing in a Notes database
- Allows use of LotusScript for advanced data manipulation
Enables self service reporting capabilities to end-users.
Click for more info. |
|
|
|
|
|
|
|
|