Comment by jsmith45
19 hours ago
For simple tail recursion, expression as a loop is pretty simple, just define the function with the same parameters, and mutate the parameters, possibly creating local copies of the original values if you need the previous value to evaluate another new parameter.
But since your example is relying on mutual tail recursion with functions for each state. Yeah, that is much harder to cleanly map to a simple loop. Like obviously making a giant function to cover all the state functions with a giant switch statement is possible, and even relatively common, but not especially clean.
If all state functions have the same parameters, then an approach with functions per state that return the new state and new parameter values, which get called from a loop using a state value to function dictionary can be relatively clean. But if states need different parameters, yeah that gets complicated.
Yes, exactly.
> If all state functions have the same parameters, then an approach with functions per state that return the new state and new parameter values, which get called from a loop using a state value to function dictionary can be relatively clean. But if states need different parameters, yeah that gets complicated.
In a language like Rust, you can encode your state in an enum, and the different variants (= states) in a Rust enum can have different parameters.
Then your implementation basically becomes a loop around a match statement over that enum.
But all you've done here is manually implement a 'trampoline'. The trampoline is to a tail call what manually maintaining a (call) stack is for simulating non-tail calls. See https://en.wikipedia.org/wiki/Trampoline_(computing)#High-le...