|
|
|
|
|
|
|
|
|
|
|
|
|
|
Using recursion in LotusScript (continued)
It's a little more complicated than our factorial example, but there are only six lines of code that require any real thought. Let's look at that If statement:
If Not Instr(currstring, replace) = 0 Then
|
The If statement is where we determine whether or not we've arrived at the end case. Are there any more back slashes present? If so, we launch into the recursive case:
lefttext = Left(currstring, Instr(currstring, replace)-1)
righttext = Right(currstring, Len(currstring) - (Instr(currstring, replace) - 1 + Len(replace)))
fulltext = lefttext + replacewith + righttext
replacesubstring = replacesubstring(fulltext, replace, replacewith)
|
The first three lines merely reassemble the string. Everything to the left of the text we want to replace is added to the text we want to replace it with. This string is then added to everything to the right. (If we were trying to replace back slashes with forward slashes in "Demo\TestDB.nsf", we'd add "Demo" to "/" and then add that to "TestDB.nsf".)
The fourth line in the recursive case is what truly makes this function recursive. We take the string we've just reassembled and check for more backslashes by recursively calling the ReplaceSubstring function.
The remainder of the important code consists entirely of:
replacesubstring = currstring
|
This is our end case, where we've finally reduced the problem to a point where it can be easily solved.
Drawbacks to using recursion While recursion can be a powerful tool, that power does come with drawbacks. The first drawback is that it takes some practice before you can start slamming out recursive code. If you're in a hurry to meet a deadline, you'll almost certainly use the Evaluate statement in LotusScript before you'd attempt to rewrite @ReplaceSubstring as a recursive function.
Another drawback is that recursive code may confuse those who aren't already familiar with recursion. Most Domino developers can muddle through someone else's LotusScript without too many problems (especially if it's well commented!). But looking at someone else's recursive code for the first time can be quite a trial.
Perhaps the biggest drawback to using recursion is the limit that Lotus Notes imposes on recursive functions. If your elegantly written recursive function needs to call itself too many times, you'll receive an "Out of Stack Space" error. This is because Lotus Notes has a 32K limit on the amount of information it stores in the stack for recursive functions. In other words, Lotus Notes won't be able to keep track of all of the temporary variables your function is using!
The first two drawbacks can be easily overcome, but that 32K-stack limit can be a real problem. Luckily, you can usually give yourself a little more stack space to play with by making fewer recursive calls. For instance, if we were to check for two back slash characters each time through our ReplaceSubstring function, we'd cut the number of recursive calls in half.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
-- Advertisement --
Sophisticated Meets Simple For Document Management
Share. Control. Manage.
Documents, emails, and content in the context of how work is done.
Native to Lotus Domino. The User Experience unseen for Lotus Domino.
Do more with less. Really.
See the possibilities Docova unleashes for Lotus Domino. |
-- 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! |
|
|
|
|
|
|
|
|
|
|