Make recursive Scala functions tail-recursive by simulating the call stack.