Search DominoPower's 11,437 Lotus-related article archive 
Home
EasyPrint
News details Click here for the RSS feed's XML code. This is not a browser URL.
Articles-only Click here for the RSS feed's XML code. This is not a browser URL.
Twitter Feed Click here for the Twitter feed.
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:

Factorial = 1

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


« Previous  ·  1  ·  2  ·  3  ·  4  ·  Next »
Other articles you might like
Home > Lotus Technologies > LotusScript (64 articles)
   When the debugger won't debug hidden code that isn't hidden
   What to do if the LotusScript debugger won't single-step over code
   Little known traps about Lotus Notes fields
Get Weekly Email Updates
Subscribe to our regular weekly email newsletter. It's packed with tips, reviews, deep analysis, and the latest news.
 
Recent DominoPower Articles
Application development, William Shatner, and the origin of the universe
Learn Domino Designer 8.5 for free
The (near) future of Sametime, Quickr, Connections, and Symphony
Inside the IBM Innovations lab
Lotusphere 2010: Hot fixes and cool news for Notes, Domino, and LotusLive
Lotusphere 2010: mobility and collaboration
2010: A Lotusphere of change
Latest Lotus Headlines
SNTT : XPages onclick Ghosts in the machine
Ports used by Lotus Sametime 8.5 servers
Exploring a Domino Date Bug
Adding Quick Highlighter support to IBM Lotus Notes Domino Wiki, Weblog, or Webpage
Remember Young Admins...there are 2 files
WebSphere Portal 6.1.0.2 and Lotus Domino 8.5
The CKEditor - with Domino
>> Read all the news
More from the ZATZ journals
Computing Unplugged: The iPad defenders have spoken
David Gewirtz Online: CNN commentary and analysis
OutlookPower: More about disappearing text
-- 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!

ZATZ Home  ·  News  ·  Back Issues  ·  Credits/Trademarks ·  Link To Us
Copyright © 1998-2010, ZATZ Publishing. All rights reserved worldwide.
Editor's Login