3 ms·
I am not nearly as knowledgeable as Guy Steele, but I attempt an answer. (I will be using Haskell-like notation, because it's most natural for me at the moment
by eru 17y ago
I am not nearly as knowledgeable as Guy Steele, but I attempt an answer.
(I will be using Haskell-like notation, because it's most natural for me at the moment. I hope currying does not confuse you.) I agree that in a lot of scenarios you can be happy with a step function
step :: State -> Input -> (Output, State)
and a loop that feeds inputs to the step function, then does something with the output (like displaying it) and repeats with the new state. Here I could argue that this loop is best described as a tail-recursive function; but I guess the loop is bearable. However what if you have more complex flow of control? Like a non-deterministic state machine. Think of a parser. There loops won't suffice and you will have to make up your own control structures out of function calls. (Or emulate them with an explicit stack. But why re-implement language features?) Composability also benefits from tail calls.
I do not know if the above paragraph contains an argument that's good enough. What do you think? I do know that parsing with combinators is pleasant to program and benefits (at least in its natural expression in FP) from proper tail calls.
I also toy with the idea of using the following definitions instead of separating the (deterministic) stepping function from the state in the interface:
date StepState = State (Input -> (Output, StepState))
(It might be a good idea to then separate state and stepping function in most implementations. In Haskell I'd also --- probably --- use typeclasses. But that doesn't add to the point here.)
- eru 17y agoP.S. http://fare.livejournal.com/142410.html http://fare.livejournal.com/142410.html seems to have some good arguments, too. (Though not on the exact same issue as this thread.) Also the comment by gls (Guy Steele I guess) http://projectfortress.sun.com/Projects/Community/blog/ObjectOrientedTailRecursion#comment-7 http://projectfortress.sun.com/Projects/Community/blog/Objec... gives an interesting perspective on the union example.
- viraptor 17y agoInteresting. Parser combinators might actually be a "proper" example that can't be transformed into either a loop or a single trampoline chain... I've never tried to implemented one, but I can imagine some parts to be easier when done directly with closures (try one path, if it fails just drop it and try another one). On the other hand - which parser-combinator needs more than 50 or so levels of depth... Hopefully someone here did it / can comment :)
- eru 17y ago> On the other hand - which parser-combinator needs more than 50 or so levels of depth... Oh, that's easy: Just have a combinator that repeats somehow, but complicated enough that you don't want to do it as a loop.