Thursday, January 13, 2011

A better recursive mood

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.

No comments:

Post a Comment