TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Admitting That Functional Programming Can Be Awkward (2007)

101 pointsby kkdaemasover 4 years ago

24 comments

attyover 4 years ago
I think anyone who has coded long enough would recognize that OOP, functional, imperative, etc all have their strengths and weaknesses, and all have problem domains they are better suited for. Unfortunately, this sometimes gets lost in the language fan wars - it’s easy to lose nuance when discussing something you are passionate about, I have made this mistake myself.
评论 #24880087 未加载
评论 #24883851 未加载
评论 #24880529 未加载
评论 #24880788 未加载
kibwenover 4 years ago
I think it&#x27;s time we stop using the phrase &quot;functional&quot; to describe this paradigm; at this point, every imperative language made in the past 30 years has first-class functions, and there is no other first-class language feature common to all the self-described &quot;functional&quot; languages.<p>IMO, a more accurate term would be <i>stateless</i> programming. This paradigm is about minimizing state. Of course, as the OP mentions, state is often quite useful. But minimizing state, especially global state, particularly mutable state, and especially particularly global mutable state, is something even imperative language fans can get behind.
评论 #24880509 未加载
评论 #24880257 未加载
评论 #24880158 未加载
评论 #24880258 未加载
评论 #24880216 未加载
评论 #24886012 未加载
评论 #24880198 未加载
评论 #24881087 未加载
eyelidlessnessover 4 years ago
Honestly I think this conclusion comes more from lack of familiarity with how people solve similar problems with FP than anything. For example, in the author’s followup post:<p><pre><code> All you have to do is pass the world state to each function and return a new state. True, yes, but...yuck. It can be clunky in a language with single-assignment. And what is this really gaining you over C? </code></pre> I mean, if you stop there, sure that’s ugly. But if you model your program with that as the foundation, then break it down (and generalize where breaking it down is general), it’s pretty easy to reason about. And what it “gains you” is never having to think about something changing in a way that produces invalid or unexpected state. (This is more true in statically typed languages of course.)<p>In any program of any real complexity, you will eventually be inclined to break the problem down into smaller pieces. If your smaller pieces are functions, you can be certain about the state that’s returned by them. If they’re stateful subroutines, then you have to think about multiple pieces at the same time.
评论 #24880559 未加载
评论 #24880586 未加载
voodootruckerover 4 years ago
I wrote an exercise once while I was teaching: &quot;pure functional pacman&quot;. Really I did it to teach myself Redux, and it was a pretty fun way to learn.<p>Totally agree with the article: sometimes functional is the right tool for the job, sometimes OOP.<p>A more recent example: parsing USB descriptors in Rust. The parser would have been trivial and easy to understand if I could have multiple mutable references to nodes, but alas I kept fighting the borrow checker.<p>I talked with a friend about why it was so hard, and he said &quot;do it in a functional style&quot;. That made my borrow checker worries go away, but instead I have multiple &quot;tree pivoters&quot; that recursively descend through input trees while building output trees. It practically broke my brain and although it is pure, it&#x27;s not nearly as readable or maintainable as one with mutation would have been.
评论 #24880250 未加载
评论 #24880141 未加载
jasperryover 4 years ago
Could it be the case that functional structure makes programs more reliable and easier to reason about in the large, but functional style&#x2F;syntax may not be the best way to write code locally?<p>Trying to wrap an entire function&#x27;s computation into one expression is a fun mental exercise, but the resulting code may be harder to read than a sequence of statements. Haskell&#x27;s &quot;do&quot; notation seems to be an acknowledgment of this, but to me it&#x27;s adding another layer of abstraction just to recover what imperative syntax gives in the first place. Why not start from the imperative model, and then find other ways to try to limit side effects and mutation? Languages like Rust may be heading in that direction; &quot;Return of the statement&quot; as it were.
评论 #24880371 未加载
评论 #24880355 未加载
karmakazeover 4 years ago
The direct update imperative way works until you have enough multithreaded actions that interact in undesirable ways. In addition, locks don&#x27;t compose. I worked on a relatively straightforward trading app with charting and realtime rates. There were so many bugs from direct updates that interfered with rendering that it was best to restructure it as world-in, apply function(s), world-out. Then the renderer only had to deal with one frame of the world. Some items had a smaller &#x27;world&#x27;, like a single price tile. Whether done in a functional language or a procedural one this functional pattern is a common one.
chowellsover 4 years ago
I would <i>much</i> rather write the logic the author describes in Haskell than C. The C code will have to be carefully managed during development and maintenance to make sure the side effects are happening in the expected order and that no changes ever introduce unexpected interleaving of mutation. The Haskell version will prevent that trivially. You can&#x27;t even express doing it the wrong way, because you can&#x27;t mutate anything.<p>Accidentally interleaved mutation is not a theoretical or academic problem. It&#x27;s probably the number three source of production bugs in my dayjob&#x27;s various products, behind misunderstood requirements and web browsers constantly changing the rules. It turns out that everyone, even people like me who know better and have been burned several times, will sometimes take the convenient shortcut. It&#x27;s so tempting to get something done immediately by quietly mixing some mutation in an unexpected place, and it usually doesn&#x27;t bite you. Then it gets ossified that way, and a year later starts biting you. This really does happen in code maintained by multiple developers over multiple years.<p>To be fair to the original post, though, Haskell has come a long way in making that kind of coding easy since 2007. The lens library didn&#x27;t exist back then, and it&#x27;s a big part of why data transformation programs are so much more pleasant in Haskell than most languages. It lets you express data access at the right level of abstraction. You get laws to enable algebraic reasoning and a broad range of utilities for composing small pieces together to solve complex problems.
评论 #24881728 未加载
dexwizover 4 years ago
The middle part about insects, their spawn rates, types, and mutating values is all about state. No matter how much you try to go &quot;stateless&quot;, there will be some state implied by your data. Functional programming as the author describes it is good at calculating state, but bad at accessing it. The only for functions to access state is through parameters, which have a final single value in your closure (no out parameters please). They don&#x27;t have registers&#x2F;variables&#x2F;properties to modify.<p>Functional programming says values like spawn rate or type should be the output of an expression. Monads try to solve this, but storage must be the output of an expression, and is always more complex than simple assignment.<p>There is one more secret about programming that functional tries to ignore, that it runs on physical machines. Even though some would like to describe programs pure mathematically, they still are bound by physical mechanisms. Most of those mechanisms are for storing state in memory or transferring state to another location. Abstracting over this fact makes programming painful once performance is taken into account.
mrkeenover 4 years ago
This article implies that functional languages cannot run imperative statements, even though it takes for granted that imperative languages can call functions. Google &quot;world&#x27;s finest imperative language&quot; for a different take on this.<p>&gt; And each of these is messier than it sounds, because there are so many counters and thresholds and limiters being managed and sounds being played in all kinds of situations, that the data flow isn&#x27;t clean by any means. &gt; What&#x27;s interesting is that it would be trivial to write this in C.<p>You could make the same argument about dealing with git and all its crazy branching, rebasing, and reflogs, compared to just editing your source code in place. Or double-entry bookkeeping versus just keeping track of how much money you have. It gets awkward.<p>I loved the elegance of game programming in Elm a few years back, but the performance was dogshit - in my case at least - so I probably wouldn&#x27;t do it again.<p>The real reason to stick to C&#x2F;C++ in game programming is speed.
评论 #24880755 未加载
flowerladover 4 years ago
What is worse is when some developers maintain and mutate global state, write functions with side effects, and still think their code is &quot;functional&quot; because they aren&#x27;t using any classes. A lot of JavaScript developers are moving to that camp because of React and Hooks.<p>More on that here: <a href="https:&#x2F;&#x2F;medium.com&#x2F;weekly-webtips&#x2F;dysfunctional-programming-in-javascript-cae5c085a76e" rel="nofollow">https:&#x2F;&#x2F;medium.com&#x2F;weekly-webtips&#x2F;dysfunctional-programming-...</a>
nicolashahnover 4 years ago
&gt; admit that functional programming is the wrong paradigm for some types of problems.<p>Absolutely. Some problems are just so much simpler in an imperative style that it&#x27;s worth the lack of confidence you get with the functional solution in its correctness and robustness.<p>That&#x27;s why I love languages that have fairly powerful functional features but have an imperative escape hatch. I think more and more languages are becoming that. Rust does it very well, but the lisps, probably Scala, and a few others I&#x27;m not thinking of may be better, don&#x27;t have much experience with those though.
评论 #24880222 未加载
评论 #24880070 未加载
dnauticsover 4 years ago
I&#x27;m confused by this article. The author says he&#x27;s in erlang (which, btw, is not pure functional). And then goes on to describe a system which sounds like an object oriented system. But a game programmer would probably actually implement it not as object per se, but maybe backed by an entity component system. But it seems like BEAM languages would be actually quite good at expressing entity systems, which, correct me if I&#x27;m wrong, are similar to say checking out and manipulating data in a database.
评论 #24880309 未加载
savanalyover 4 years ago
&gt;Sure, there have been games written in Lisp and some games written by language dilettantes fond of Objective Caml, but they never turn out to be programmed in a functional style. You can write imperative code in those languages easily enough.<p>I would like to know more about this. I&#x27;ve written several games in Elm, a js counterpart to Haskell, and I guess foolishly assumed that meant I was writing them in the functional style. Now I&#x27;m curious if I&#x27;m actually writing in an iterative style and if so what would the functional style look like? I would welcome general responses to this as well as any comments on the code style specifically in examples like [0].<p>[0] <a href="https:&#x2F;&#x2F;github.com&#x2F;tristanpendergrass&#x2F;legendary-barnacle&#x2F;blob&#x2F;master&#x2F;src&#x2F;Main.elm#L241-L279" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;tristanpendergrass&#x2F;legendary-barnacle&#x2F;blo...</a>
评论 #24882059 未加载
评论 #24880162 未加载
评论 #24880174 未加载
评论 #24880167 未加载
r-wover 4 years ago
Ever heard of the Elm Architecture? It helps address some of these pain points, e.g., by forcing you to decide from the get-go what your global modifiable state comprises, and then having each action consist of an update to that state model.
tomeover 4 years ago
Can someone with experience of entity component systems (ECS) please chime in? My understanding is that games are <i>not</i> written in the manner that the author says is so easy, rather they are written as pure functions operating on a big table of world state. The world state happens to be mutable for efficiency, but that&#x27;s an implementation detail. Then again I&#x27;ve never worked with ECS so perhaps this my misconception.
评论 #24880571 未加载
评论 #24881128 未加载
FpUserover 4 years ago
Of course it can be awkward as it is very specific and opinionated paradigm. It may help to solve some problems while being totally unsuitable to solve others in a sane manner. There are no silver bullets in life.<p>I personally always shied away from adopting&#x2F;committing myself to any strict concept. My long experience taught me that as soon as you do there it will likely blow up in your face one or the other way some time down the road.
ookdatnogover 4 years ago
John Carmack commented quite extensively on functional programming in video games:<p><a href="https:&#x2F;&#x2F;youtu.be&#x2F;1PhArSujR_A?t=125" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;1PhArSujR_A?t=125</a><p><a href="https:&#x2F;&#x2F;www.gamasutra.com&#x2F;view&#x2F;news&#x2F;169296&#x2F;Indepth_Functional_programming_in_C.php" rel="nofollow">https:&#x2F;&#x2F;www.gamasutra.com&#x2F;view&#x2F;news&#x2F;169296&#x2F;Indepth_Functiona...</a>
kaashmoneeover 4 years ago
I&#x27;m not sure I&#x27;m understanding the point of this article. Every language and language paradigm is awkward if it&#x27;s used for purposes it&#x27;s not meant for. Writing a game is something that is really heavily dependent on mainting some sort of state and functional programming in principle is supposed to be stateless. So, unsurprisingly, it&#x27;s awkward to use FP for game development.
mikewarotover 4 years ago
Programming without destructive updates works because it makes explicit the data flow in a program, not because destructive updates are bad.
ngcc_hkover 4 years ago
In spite of its name this video use JavaScript to implement a game using the three styles of programming. Great video learnt from hacker news: <a href="https:&#x2F;&#x2F;youtu.be&#x2F;vK1DazRK_a0" rel="nofollow">https:&#x2F;&#x2F;youtu.be&#x2F;vK1DazRK_a0</a>
pmarreckover 4 years ago
He thinks passing around the world state as a functional immutable data structure is “gross”, but modifying the same state as a global from anywhere in the program... isn’t?
pg_botover 4 years ago
Once you grok the language you can easily model the type of situations that they find awkward.<p>For example in erlang&#x2F;elixir you can keep game state in a series of processes. You model the state changes based on messages that a process receives, and you can spawn&#x2F;terminate processes as needed. Everything he described as challenging is trivial if you understand the language paradigm.
jqpabc123over 4 years ago
There is no one &quot;best&quot; way to program.
nerdtimeover 4 years ago
Functional programs tend to be more modular than imperative or OOP counter parts but nobody really knows why nor do they understand the cases where FP becomes less modular.<p>FP is only modular when you use combinators. If you use closures then it&#x27;s no longer modular.<p><pre><code> f x y = x + y g y = y * 2 w x = (f x) . g </code></pre> g and f are combinators and modular and w is the composition of both of those combinators.<p><pre><code> w = \x -&gt; (\y -&gt; (x + y) * 2) </code></pre> In this case the above is not modular because it doesn&#x27;t use combinators. The above style is actually kind of promoted by haskell when you need to do things with side effects. It actually makes FP more complex than it needs to be without improving modularity.
评论 #24881531 未加载