Today I thought about my palindrome detection function, and revised it.
func bool IsPalindrome(s)
return true if s.length < 2
return false if s[0] != s[-1]
return IsPalindrome(s.substring(1, s.length - 1))
end
This version separates the "no brainer" conditions from the recursive call. It also uses tail recursion.
Showing posts with label recursion. Show all posts
Showing posts with label recursion. Show all posts
Thursday, January 13, 2011
Subscribe to:
Posts (Atom)