I am trying to understand how various recursive functions can be converted to tail recursive. I've looked through the many examples of both fibonacci and factorial conversions to tail recursive and understand those but am having a tough time making the leap to a problem with a somewhat different structure. An example is:
def countSteps(n: Int): Int = {
if(n<0) return 0
if(n==0) return 1
countSteps(n-1) + countSteps(n-2) + countSteps(n-3)
}
How would you convert this to a tail recursive implementation?
I've looked through similar questions such as: Convert normal recursion to tail recursion But again these don't seem to translate to this problem.