In a time where we're finally realizing the benefits of functional programming (i.e. no side effects), a problem like this really seems like a callback to a more barbaric era. Like writing a state machine using gotos.
EDIT: Never expected this to devolve into an argument about the merits of gotos! I thought we were all past that, but apparently not.
Gotos are a natural choice for state machines, we basically emulate goto when we use while+switch or while+function pointer. The "goto considered harmful" paper is about structured programming, saying that you should use structured programming instead of goto, but it doesn't apply here because goto is the right structure to begin with.
I suggest reading the actual "goto" paper sometime… it's a classic.
I just want to add that the "goto" criticized on that paper does not exist anymore in any modern language (that includes C). Goto has been nerfed into something that can not destroy structured programming.
But I do think the while+switch representation is superior.
The state is data either way... either an explicit variable or the program counter. With goto, the compiler is more likely to be able to see across state change boundaries (e.g. phi() will work correctly without doing anything fancy). So you can, e.g., have the compiler help you out by giving you warnings for uninitialized variables, when the variables are initialized in one state and used in another.
No... you can only longjmp if the jmp_buf corresponds to a setjmp created within a function call which hasn't returned yet and in the same thread. I would call this a primitive form of structured programming with some serious limitations--it's kind of like throwing an exception, but more primitive, and it's kind of like the Common Lisp block/return-from, except less convenient, and it's kind of like call/cc but with a bunch of limitations added.
You can do some dangerous things with longjmp to implement coroutines, but it's horribly non-portable.
Pattern matching is so much sexier than switch statements.
Writing state machines is one of the moments when the 1d top-down method of programming really hurts. I feel like I can easily understand an FSM drawing, but splay it out in code and it's difficult to track.
Asking honestly because I would like to know: What is wrong with writing a state machine using gotos? Assuming that one is solving a problem that definitely needs a state machine for some reason.
Many programmers treat 'goto' the same way that we treat radioactive waste, but they don't know why. Perhaps it is a holdover from courses with teachers who automatically graded 'fail' on programs that used a goto. Perhaps the programmers have only heard that gotos were bad.
Sometimes 'goto' is the correct answer. For state machines it can be an pretty efficient technique. For "ordinary" code it is sometimes the best way out of a situation (e.g., busting out of a deeply nested set of loops).
The "Goto considered harmful" movement came out of the 1970s, when GOTO was used all the time and the normal state for a big project was abject spaghetti. I think we can all agree that was a very bad thing. But the utter avoidance of goto is an overreaction that seems to be based on unreasoning fear, or toxic peer pressure.
so, goto is efficient at escaping inefficiently structured code? That doesn't make it really efficient. I mean there's a reasonable rule of thumb: don't indent the code too deep. Because what you are talking about is one kind of spaghetti, the kind cut into digestable chunks. The interleaving of active and inactive blocks leads to unreadable code sooner or later.
This is lazy thinking, the gotos described in that essay can jump anywhere in the program while modern gotos are limited to the function scope and have various warnings about initialization order and whatnot.
All the arguments it makes no longer apply but people still quote the snappy headline.
That essay does make an excellent argument against programming with many tiny functions though, as those do cause the execution cursor to jump all over the source code document.
It seems to me that a substantial fraction of the people who quote that paper, assuming they have even read it, have failed to think critically about it.
You could always clone the tree into a new linked-list if you wanted to keep things purely functional, but that would add needless complexity and still not make it easier to create a correct implementation.
Functional programming as a paradigm makes sense when you want to limit the context a programmer has to be aware of when working on a problem, but in this case recursion and imperative programming accomplish the same goal.
EDIT: Never expected this to devolve into an argument about the merits of gotos! I thought we were all past that, but apparently not.