Strange Attraction
Monday, January 28th, 2008As the old saying goes, “it’s the little things in life that matter.” Well one of the little things in my life is recursion. What is recursion, you ask? Well if you’re programmer you probably know what it is, and if you aren’t you wouldn’t understand even if I explained it. Just know that recursion is great, because it’s simple, elegant, and extremely efficient. The only downside to it is that it’s kind of hard to understand at first look. Unfortunately in today’s coding environment of bloatware with objects and resources being allocated everywhere with little prior thought, coding techniques like recursion often get lost in the shuffle.
Well today I actually got to use a bit of recursion to accomplish a bit of string manipulation. Basically I had to strip off all the leading characters of a string that were alpha characters (letters). Needless to say, it made me smile to see something accomplished so elegantly. For those that care to see it, here’s the (badly parsed… thanks WordPress) code. It’s written in Visual Basic .Net (not my choice):
Private Function stripLetters(ByVal word As String) As String
’ Recursion, everyone cheer!
If Char.IsLetter(word.Chars(0)) Then
Return stripLetters(word.Substring(1))
Else
Return word
End If
End Function
That, folks, is a perfect function in every way, and I don’t get to write them too often.