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.

Functor, Applicative, and Monad

252 pointsby TheAsprngHackerover 5 years ago

16 comments

wh0knowsover 5 years ago
I don&#x27;t think this article is very useful. It doesn&#x27;t adequately provide an introduction to OCaml code (or adequately explain what a given code snipped is doing) and yet frequently defers to just code to explain a concept. It&#x27;s an unrealistic expectation to expect an unfamiliar reader to simultaneously infer what a particular code snippet is doing then also go a level deeper and understand the concept that is trying to be presented.<p>This article is most readable to those who already understand OCaml code, and if you can already read OCaml code you already understand these concepts.
评论 #21002816 未加载
评论 #21003345 未加载
评论 #21005861 未加载
评论 #21003676 未加载
评论 #21008090 未加载
评论 #21006167 未加载
评论 #21011148 未加载
评论 #21003880 未加载
评论 #21004446 未加载
hardwaregeekover 5 years ago
An important realization that I had was that monads&#x2F;functors&#x2F;applicatives aren&#x27;t patterns in the sense of design patterns. You don&#x27;t solve a singular problem with a monad. With something like a strategy pattern you have a concrete problem: how do I select different potential algorithms? Monads don&#x27;t have a specific problem that they solve. Any attempt to motivate monads in such a manner falls flat because the problem is either too general to be motivating or too specific to apply to monads as a whole.<p>Instead, functors&#x2F;monads&#x2F;applicatives are more like a technique that can be used to solve a wide variety of problems that all coincidentally use the same function signature. And therefore, it&#x27;s perfectly acceptable to say &quot;I know how monads work with Maybe and List, but not Reader&quot; Because fundamentally, how a Reader implements bind is in no way related to how Maybe or List implement bind.
评论 #21002620 未加载
评论 #21004576 未加载
mesarvagyaover 5 years ago
Even better <a href="http:&#x2F;&#x2F;adit.io&#x2F;posts&#x2F;2013-04-17-functors,_applicatives,_and_monads_in_pictures.html" rel="nofollow">http:&#x2F;&#x2F;adit.io&#x2F;posts&#x2F;2013-04-17-functors,_applicatives,_and_...</a>
评论 #21002563 未加载
urygaover 5 years ago
there&#x27;s an equivalent definition of Applicative that may be a bit less intimidating:<p><pre><code> class Functor f =&gt; Applicative f where unit :: f () (**) :: f a -&gt; f b -&gt; f (a,b) </code></pre> [source - a bit CT heavy](<a href="https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;35013667&#x2F;5534735" rel="nofollow">https:&#x2F;&#x2F;stackoverflow.com&#x2F;a&#x2F;35013667&#x2F;5534735</a>)<p>(i&#x27;ll be writing the second operation as `××` in some places because of HN asterisk weirdness)<p>this gives us:<p>- `unit`, a &quot;template&quot; container with a hole we can fill (by doing `fmap (const myValue) unit`, equivalent to `pure myValue`)<p>- `fa ×× fb`, to &quot;compose&quot; two Applicative values. this shows how, unlike with Monads, the two computations must be &quot;independent&quot;. for example, if IO were only an Applicative, you could do<p><pre><code> print &quot;hello&quot; ** print &quot;world&quot; </code></pre> but not<p><pre><code> getLine &gt;&gt;= \s -&gt; print (&quot;hello, &quot; ++ s) </code></pre> (where the second computation depends on the string we got in the first one)<p>it also nicely shows the two ways lists can be an Applicative - `fa ×× fb` can be either the Cartesian product `[(a,b) | a &lt;- fa, b &lt;- fb]` or `zip a b` (ZipList).<p>(also, it&#x27;s kind of like a Monoid, which is neat!)
评论 #21004320 未加载
hope-strikerover 5 years ago
Nice explanation of monads!<p>My two cents: personally, I would&#x27;ve started with the fact that a monad is exactly the stringing together of functions (a -&gt; m b), and the similarity between monoids, monads, strings &#x2F; lists and functions under composition. You mentioned that<p>• (a -&gt; [b]) is the type of non-deterministic computations<p>• (a -&gt; Maybe b) is the type of fallible computations<p>• (a -&gt; IO b) is the type of effectful computations<p>• (a -&gt; (s, b)) is the type of computations with state s<p>• that the Monad instance merely specifies how to compose them<p>• all such composable constructs can be expressed as a monad<p>• do notation and list comprehensions automatically work across all of them<p>and, in my opinion, these are all much more powerful motivation than beginning with a comparison to functors.
BucketSortover 5 years ago
It&#x27;s just mind boggling how universal these concepts are and how they show up in surprising ways. As a recent example, I&#x27;ve been learning the basics of composing music in Haskell with Euterpea[1] and wanted to make a function which played several notes over a list of octaves to make chords. It turns out the applicative operator was exactly the function I need to do this! It would be hard to go into the details in just a little blurb here... but here is a piece I made with with it[2]. And here&#x27;s the line of code with the applicative operator:<p>notePlayer notes octs dur = musicSeq $ (uncurry &lt;$&gt; notes) &lt;*&gt; ((, dur) &lt;$&gt; octs)<p>Won&#x27;t make much sense without context, but it&#x27;s there!<p>[1]: <a href="http:&#x2F;&#x2F;www.euterpea.com&#x2F;" rel="nofollow">http:&#x2F;&#x2F;www.euterpea.com&#x2F;</a><p>[2]: <a href="https:&#x2F;&#x2F;soundcloud.com&#x2F;a-mathematical-way&#x2F;not-enough-time-to-read-the-manual" rel="nofollow">https:&#x2F;&#x2F;soundcloud.com&#x2F;a-mathematical-way&#x2F;not-enough-time-to...</a>
评论 #21002599 未加载
评论 #21002554 未加载
larussoover 5 years ago
I always go back to “Learn you a Haskell for Great good”.<p><a href="http:&#x2F;&#x2F;learnyouahaskell.com&#x2F;functors-applicative-functors-and-monoids" rel="nofollow">http:&#x2F;&#x2F;learnyouahaskell.com&#x2F;functors-applicative-functors-an...</a>
naccover 5 years ago
Being someone who is still struggling with these concepts, I like this tutorial because at least it doesn&#x27;t use the common list&#x2F;maybe&#x2F;state to illustrate the concepts. Somehow I feel these concepts are so abstract - unless one is well versed in category theory, maybe only a data approach can prevent people from overfitting these concepts to specific examples.<p>I would really hope to see a tutorial that have a diverse set of examples and just fmap each example with a light explanation of say, what is a monad in this code and what is not, and because it&#x27;s a monad we can do this.<p>Essentially the tutorial can just train a classifier in one&#x27;s head, and with a nice set of examples maybe the brain can learn a general representation of concepts for the classifier ...
评论 #21003376 未加载
评论 #21003349 未加载
ollysbover 5 years ago
After learning Elm for a few months I picked up a Haskell book. When I got to the Functor, Applicative and Monad chapters I discovered that I’d actually been using them for months without knowing it. Having had practical experience with them the theory came as a revelation. I found that I’d developed an intuition for them from all the concrete scenarios where I’d used them.<p>The Elm community goes out of it’s way to avoid talking about them because they make learning functional programming seem far more intimidating. It’s a very simple language though - Evan had always had beginners in mind when desiging the language and tools. If you’re looking to improve your functional programming skills it’s got a great learning curve.
hosejaover 5 years ago
I should learn to read Haskell one of these days. Not today though.
评论 #21002662 未加载
评论 #21004368 未加载
评论 #21002777 未加载
voidhorseover 5 years ago
I still find Philip Wadler’s original paper on Monads to be the clearest explanation of the pattern and concept. It remains scoped to the usefulness of the concept in programming, lays out very clear motivating examples, and proceeds to implement the pattern to solve each case in a lucid and explocit manner. I’d say the only downside is that it assumes at least some familiarity with FP and writing more “theoretical” or academic tools like evaluators, but all in all it’s still much clearer than the majority of garbled explanations of monads, even those that attempt to explain the concept through its dependencies&#x2F;priors (functor&#x2F;applicative).<p>here’s the paper: <a href="https:&#x2F;&#x2F;homepages.inf.ed.ac.uk&#x2F;wadler&#x2F;papers&#x2F;marktoberdorf&#x2F;baastad.pdf" rel="nofollow">https:&#x2F;&#x2F;homepages.inf.ed.ac.uk&#x2F;wadler&#x2F;papers&#x2F;marktoberdorf&#x2F;b...</a>
moominover 5 years ago
<p><pre><code> It turns out that every generic type t has a corresponding map function map : (&#x27;a -&gt; &#x27;b) -&gt; &#x27;a t -&gt; &#x27;b t. </code></pre> So how about x : &#x27;w -&gt; Bool?
评论 #21004345 未加载
leshowover 5 years ago
&gt; According to currying, &#x27;a -&gt; &#x27;b -&gt; &#x27;c is interchangeable with &#x27;a * &#x27;b -&gt; &#x27;c. (The fancy math word for this interchangeability is &quot;isomorphism.&quot;)<p>I thought isomorphism was when a function was reversible. I didn&#x27;t think it had anything to do with currying.
评论 #21005133 未加载
评论 #21005623 未加载
_bxg1over 5 years ago
I&#x27;ve always understood intuitively the existence of .map(), .reduce(), and .flat() in JavaScript, but .flatMap() felt like a weirdly arbitrary combination of two of them. Now it makes sense!
foobar_over 5 years ago
Maths is what you get when you limit all your variable names to single letters.
评论 #21005994 未加载
评论 #21003548 未加载
harry8over 5 years ago
Pretty weird that a comment linking a chapter from an oft referred to haskell text is a dead comment here. Surely if the alternate explantion in &quot;Learn You a haskell for great good&quot; is somehow sub-optimal it would be better to explain how rather than kill the comment inside 10 minutes?<p>You see this sort of thing from language warriors fighting silly wars but, yeah, what&#x27;s wrong with Learn You a Haskell? Why must it be fought and suppressed immediately? Crazy...
评论 #21003493 未加载
评论 #21002558 未加载