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.

Functors, Applicatives, and Monads

84 pointsby abhi9uabout 2 months ago

14 comments

lihaoyiabout 2 months ago
For some reason everyone likes to talk about Monads, but really the other types here are just as interesting. For example, Applicatives are less dynamic than Monads in that you can&#x27;t `flatMap`&#x2F;`bind` to decide on the &quot;next&quot; thing to evaluate based on the previous value, but in exchange you get a &quot;static&quot; tree (or graph) of Applicatives that lends itself much better to static analysis, optimization, parallelism, and so on.<p>IIRC Haxl (<a href="https:&#x2F;&#x2F;github.com&#x2F;facebook&#x2F;Haxl" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;facebook&#x2F;Haxl</a>) uses Applicatives to optimize and parallelise remote data fetching, which is hard to do with Monads since those are inherently sequential due to the nature of `flatMap`&#x2F;`bind`. My own Mill build tool (<a href="https:&#x2F;&#x2F;mill-build.org&#x2F;" rel="nofollow">https:&#x2F;&#x2F;mill-build.org&#x2F;</a>) uses an applicative structure for your build so we can materialize the entire build graph up front and choose how to parallelize it, query it, or otherwise manipulate it, which is again impossible with Monads since the structure of a Monad computation is only assembled &quot;on the fly&quot; as the individual steps are being evaluated. &quot;Parallel Validation&quot; where you want to aggregate all failures, rather than stopping at the first one, is another common use case (e.g. <a href="https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;validation" rel="nofollow">https:&#x2F;&#x2F;hackage.haskell.org&#x2F;package&#x2F;validation</a> or <a href="https:&#x2F;&#x2F;typelevel.org&#x2F;cats&#x2F;datatypes&#x2F;validated.html" rel="nofollow">https:&#x2F;&#x2F;typelevel.org&#x2F;cats&#x2F;datatypes&#x2F;validated.html</a>)<p>Monads seem to have this strange aura around them that attracts certain kinds of personalities, but really they&#x27;re just one abstraction in a whole toolkit of useful abstractions, and there are many cases where Applicative or some other construct are much more suited
评论 #43526237 未加载
评论 #43528364 未加载
jerfabout 2 months ago
Unfortunately, while you may not have appreciated the tone of the Haskell interaction, they are correct in their assessment from a factual perspective. This explanation propagates a number of misunderstandings of the topics well known to be endemic to beginners.<p>In particular, I observed the common belief that functors apply to &quot;containers&quot;, when in fact they apply to things that are not containers as well, most notably functions themselves, and it also contains the common belief that a monad has &quot;a&quot; value, rather than any number of values. For instance, the &quot;list monad&quot; will confuse someone operating on this description because when the monad &quot;takes the value out of the list&quot;, it actually does it once <i>per value</i> in the list. This is the common &quot;monad as burrito&quot; metaphor, basically, which isn&#x27;t just bad, but is actually wrong.<p>I&#x27;m not limiting it to these errors either, these are just the ones that leap out at me.
评论 #43528846 未加载
评论 #43525947 未加载
评论 #43527534 未加载
评论 #43532481 未加载
评论 #43527337 未加载
评论 #43526893 未加载
rthnbgrredfabout 2 months ago
This reminds me of <a href="https:&#x2F;&#x2F;www.adit.io&#x2F;posts&#x2F;2013-04-17-functors,_applicatives,_and_monads_in_pictures.html" rel="nofollow">https:&#x2F;&#x2F;www.adit.io&#x2F;posts&#x2F;2013-04-17-functors,_applicatives,...</a><p>I think over the recent years, there&#x27;s been a rise in typed languages that support functional programming like TypeScript and Rust. It will be interesting to see if this trend continues in the context of AI assistant programming. My guess is that it will become easier for beginners, and the type systems will help to build more robust programs in cooperation with AI.
评论 #43528712 未加载
评论 #43529036 未加载
burlesonaabout 2 months ago
I feel like Haskell is easier to use than it is to explain, and in my experience a lot of these kind of tutorial &#x2F; explanations actually make things seem harder and more complicated than just working with the concepts and observing what they do. (This one included.)
评论 #43526480 未加载
评论 #43527086 未加载
评论 #43526451 未加载
IshKebababout 2 months ago
The problem with Monads etc. is that they&#x27;re simple concepts with extremely confusing names. Monad should be FlatMappable. Once it has the correct name it barely even needs an explanation at all.
评论 #43527910 未加载
评论 #43528080 未加载
评论 #43528593 未加载
评论 #43527469 未加载
deterministicabout 2 months ago
Monads in Haskell is just a way to combine two functions into one when the output of the first function doesn&#x27;t match the input of the 2nd function.<p>That&#x27;s all there is to it.<p>It is <i>not</i> a way to &quot;box values&quot;. Yes you can use it for that if the functions you combine happens to operate on &quot;boxed values&quot; (like Maybe) but that has nothing to do with the fundamental idea of what a monad is.<p>And yes there are &quot;monad rules&quot; that ensures that combining the functions &quot;makes sense&quot;. But people sometimes use monads in Haskell that doesn&#x27;t actually follow those rules. But it works fine anyways for whatever they are doing.
e-dantabout 2 months ago
Part of why monads are not interesting to talk about is that they’re generic enough that most explanations are incomplete, and sufficient explanations are boring and unhelpful.<p>But the biggest reason is that they’re sort of intuitive, plenty examples exist. And then at some point someone tells you that those things are monads, but it’s in the kind of way that social psychologists make up some fancy word for crap we all <i>know</i> about in our gut.<p>Nobody gives a shit that a list is a monad, people give a shit that it’s a list. Anyone who’s written lisp or node or any nontrivial C program or anything with coroutines or anything with concurrency can and <i>will</i> tell you that, yeah, duh, control flow can be represented by a data structure. A couple more fancy “monad laws” and you have something that looks like other monads, and lists and if expressions and IO meld together. Ok, how unhelpful.
评论 #43527371 未加载
评论 #43527310 未加载
评论 #43528612 未加载
mncharityabout 2 months ago
Hmm. I&#x27;ve not yet seen a topical presentation which embeds a tweaked chatbot. Graphics, video, interactive graphics, each provide additional leverage beyond text. So too might &quot;something to talk over the topic with&quot;. Something with a punch-list of insights to be conveyed, and misconceptions to be probed for.<p>Monad education is rich in flawed models and incomplete appreciation, and also in meta discussion of these. Might this lend itself to a interactive socratic-y tutor? Could the world use a... new and improved monad tutorial?
randomstateabout 2 months ago
Coming from non-Haskell background, it took me a good while to undestand that `Just` is a constructor specific to the `Maybe` type. Found this for a quite nice answer: <a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;18809252" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;18809252</a>
personperson69about 2 months ago
the bit at the end is quite rude of the haskeller responding but I also think they&#x27;re largely right; another monads explained through boxes tutorial is not gonna help anyone. In fact it&#x27;s really a step in the wrong direction. Using a few different monads is where to start.
评论 #43526150 未加载
layer8about 2 months ago
&gt; A functor is an abstraction that allows for mapping a function over values inside a context without altering the context of the functor.<p>I’m not sure this is intelligible to laypeople. ;)
rebeccaskinnerabout 2 months ago
I think it&#x27;s great that people are excited about Haskell and want to write about it, and it&#x27;s unfortunate that the author had to deal with a less thank tactful response to their work. I hope the author keeps spending time with Haskell and continues to make time to try to write more and help other people!<p>That said, here is a bit of a long comment on my thoughts about writing about and teaching these things:<p>It&#x27;s true that teaching Monads, Applicatives, and Functors can be tricky and there are a lot of articles that end up doing more harm than good- either by teaching things that are outright incorrect, or more often, teaching people a particular way to use them but setting people up for a lot of trouble when they run across uses that diverge significantly from the mental model they&#x27;ve built up.<p>Functions are a classic example of this. There useful definitions of Functor, Applicative, and Monad for functions, and depending on the mental model you&#x27;ve built up they can be either fairly easy to understand or very difficult to understand. This ends up being a big problem because Applicative and Monadic functions are so pervasive, but they are incomprehensible if your stuck in the traditional data structure mental model. IO is a great example of this- it&#x27;s really just a specialized State, but it can be really hard to understand how it works if you&#x27;re thinking about data structures. Parsers are another good example.<p>I generally prefer to start people off with the &quot;monad-as-computation&quot; mental model, roughly &quot;An `m a` is an m-computation that can have side effects and when evaluated returns a value of type a&quot;, where Maybe are computations that could fail, Lists are computations that can return multiple times, and IO are computations with all of the normal IO side effects.<p>Starting with IO has the nice benefit that you can also help people come to terms with monadic IO as a means of dealing with lazy evaluation. It&#x27;s a good gateway both into helping people come to terms with the challenges of lazy IO, and it also helps to provide a concrete motivation for IO in haskell that doesn&#x27;t result in people going off thinking that Monads are a hammer and every problem in the world is a nail.<p>From there, I think it&#x27;s helpful to talk not just about bind but also join. Showing someone how to implement join in terms of bind and vice versa is a nice thing to do early because it helps to differentiate Monad from Applicative and it demystifies the &quot;a monad is a monoid in the category of endofunctors&quot; thing a bit (not that I&#x27;d proactively bring that up when teaching someone how to use them).<p>I like to characterize the high level difference as something like &quot;Monads are computations that can _call out to_ other computations and integrate their results&quot;, &quot;Applicatives can run computations in parallel and combine the resulting structures &#x2F; side effects&quot;, and &quot;Functors allow you to lift pure functions into a computation&quot;. At each step, highlighting both how you are getting less powerful (because you can implement functors in terms of applicatives, and applicatives in terms of monads, but not the other way around), and how having less power can help you reason better about your programs (pros&#x2F;cons of applicative vs. monadic parsers are a good example here).<p>Finally, I think it&#x27;s important early on to make sure your reader understands higher kinded types. A lot of people are used to languages with generics, but many of those languages aren&#x27;t expressive enough to let you express something like Functor, and people often lack practice in thinking about something like `Maybe` separately from `Maybe Int` or `Maybe a`.<p>In the end, I think these things really aren&#x27;t that complicated, but they are built on a different view of programming that a lot of readers have the first time they encounter them, and the best approach isn&#x27;t to translate the concepts into something people are already familiar with. Instead, I think you need to help the reader adapt their mental model. It&#x27;s a harder path, but one that I think pays off more in the long run.
behnamohabout 2 months ago
monads are the MCP of functional programming—no one really knows what they are but everyone writes an article about them using analogies that break when you actually use them in practice.
评论 #43527103 未加载
评论 #43526887 未加载
hu3about 2 months ago
This is how Chat GPT o1 would explain Functors, Applicatives and Monads to a PHP developer. Looks more digestible to me, supposing it is correct.<p><a href="https:&#x2F;&#x2F;chatgpt.com&#x2F;share&#x2F;67e9b3b0-52a8-8001-87d1-d6d222a27ec4" rel="nofollow">https:&#x2F;&#x2F;chatgpt.com&#x2F;share&#x2F;67e9b3b0-52a8-8001-87d1-d6d222a27e...</a><p>The prompt to save you a click: &quot;I&#x27;m an experienced PHP developer, explain Monads to me using PHP exmaples.&quot; (yes I made a typo in exmaples but it worked fine anyway).