4 ms·
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 mor
by brianberns 9y ago
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.
- nialo 9y agoAsking 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.
- brianberns 9y agoGotos are generally considered a bad idea in high-level programming. http://www.u.arizona.edu/~rubinson/copyright_violations/Go_To_Considered_Harmful.html http://www.u.arizona.edu/~rubinson/copyright_violations/Go_T...
- CoolGuySteve 9y agoThis 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.
- _jal 9y agoIt 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.
- mcguire 9y agohttps://maniagnosis.crsr.net/2011/11/link-o-day-donald-knuth-tony-hoare-and.html https://maniagnosis.crsr.net/2011/11/link-o-day-donald-knuth...
- cabalamat 9y ago> What is wrong with writing a state machine using gotos? Nothing. But the thing they want to do -- altering the datastructure on the fly -- is not the best way to solve the problem. I would solve this by writing two functions: one that creates an array from a binary tree, and one that creates a doubly-linked list from an array. Would this use up more RAM and take longer to run? Yes Would the code be easier to understand (and therefore more bug-free)? Yes
- kabdib 9y agoNothing wrong with goto, as long as it's clear. 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.
- posterboy 9y ago>busting out of a deeply nested set of loops 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.
- eternalban 9y agoIt's "barbaric", kinda like eating with your hands. Of course this type of dogmatism gives us FP gems akin to [eating] a piece of Sushi with a steak knife.
- klodolph 9y agoGotos 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.
- marcosdumay 9y agoI 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.
- klodolph 9y agoThe while+switch, to me, looks like goto with extra typing. state = START; break; Translates directly to: goto start; Except while+switch is nicer if you have transition tables. YMMV
- marcosdumay 9y agoYes, but it forces you to keep your state as data, and makes it much more clear how the data relates to the code. But it does make for the same exact behavior.
- klodolph 9y agoThe 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.
- zokier 9y ago
- mcguire 9y agoPssst! Lambda, the ultimate goto. http://library.readscheme.org/page1.html http://library.readscheme.org/page1.html
- deleted 9y ago[deleted]
- Zelizz 9y agoYou 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.