TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

On Explaining Monads

28 点作者 emilyst将近 11 年前

5 条评论

stormbrew将近 11 年前
Not gonna lie, I was hoping for something funnier when I clicked this, because I find the endless stream of terrible examples and explanations for what a monad is extremely funny. Maybe that makes me a terrible person, I&#x27;m not sure.<p>But I think this is a rare useful addition to the corpus on layman understanding of &quot;what is a monad?&quot; Because it goes to an effort to try to destructure the problem and attack it from the angle of &quot;why is a monad useful?&quot;, the answer to which, to most people, amounts to &quot;it probably isn&#x27;t to you right now, but it might be someday.&quot;<p>What it really comes down to, though, is that a monad is a way for you to define a program that actually Does Useful Things (like ask questions and act on them and output answers and occasionally tell you you&#x27;re doing it wrong). You can use a C++ compiler or a ruby interpreter for this, or you can write a program in Haskell that generates one. And if you do the latter, monads are the construct that you build them with.<p>From there you have to explain why there&#x27;s a benefit to going the roundabout route to useful things, but no one&#x27;s going to understand them until they understand that basic premise.
评论 #7798553 未加载
评论 #7798542 未加载
chrismorgan将近 11 年前
Good job on the permalink for the Wikipedia article. I wish more people did things like that. (e.g. links to blobs in GitHub are typically identified by the branch name rather than the commit—use the commit number by pressing the y key.)
tikhonj将近 11 年前
No!<p>EDIT: As a side note, I hope my tone doesn&#x27;t come across as too harsh. I feel a bit bad about writing this way, but it&#x27;s just something I&#x27;ve been thinking about—and frustrated by—for a while. I have no problems whatsoever with the author of this piece. I just want to make sure that people really pick up on where it&#x27;s off the mark, and I&#x27;m a bit frustrated—largely with myself—that there is no good explanation of functional programming and monads that avoids many of these problems.<p>That is <i>not</i> the way to think about monads. It&#x27;s an unfortunate failure of education&#x2F;PR that people hang on to this notion. The way monads are often presented, it&#x27;s a very natural conclusion. But it&#x27;s also entirely misleading.<p>Monads are not tied to functional programming. They are not tied to <i>pure</i> functional programming. They are <i>not</i> about introducing IO effects to otherwise pure programs.<p>There is <i>one</i> that does that. One type that&#x27;s a monad and that manages effects in Haskell. IO. But that is the IO type. Other monads? Other code using monads? It often has <i>nothing</i> to do with adding effects to a pure language! (Unless you stretch the definition of &quot;effect&quot;, which, to be fair, many people do.)<p>There are lots of other monads. Lots! You can think of most Linq code as using <i>some</i> monad—but never for effects. It&#x27;s in C♯, after all. Practically Java, and about as far from pure as you can get. Promises, in JavaScript, form a hobbled monad, which could trivially be a completely reasonable and correct monad if the people writing the spec weren&#x27;t violently opposed to it.<p>The Wikipedia description... was not bad. Even if the author found it confusing. It wasn&#x27;t great either. Even that was too narrow.<p>I like to think of it like this: a monad gives us a new, custom, way to <i>compose</i> things. In the case of IO, it does, indeed, give us a way to compose effectful programs. In the case of List, it gives us a way to compose nondeterministic functions (ie backtracking). In the case of Maybe, it lets us compose functions that may fail (ie early termination). And so on.<p>And the final paragraphs showcase yet another unjustified belief: that pure functional programming is basically just imperative programming <i>but less</i>. That&#x27;s something I really need to write about <i>at length</i>. Suffice to say, it&#x27;s more enlightening to think of pure functional programming—especially like Haskell—as almost <i>orthogonal</i> to imperative programming, not as a subset.<p>But, again, this is not something clear from most Haskell learning materials or evangelism. It&#x27;s just so easy to describe it as imperative programming without assignment or imperative programming without side effects or... I did the same thing myself when I started. But that turned out not to be a good description at all. If anything, it just serves to confuse as soon as you get to intermediate or advanced Haskell topics.<p>I completely understand why people take this tack. The best way to learn is by drawing connections and analogies to what you know. And Haskell learning materials don&#x27;t exactly provide the contrast or elucidation you&#x27;d need to avoid many of these pitfalls. I&#x27;m not sure what the best approach to rectifying this is, so for now I&#x27;ll just write a few blog posts about it and not much more...<p>If you&#x27;re curious, I&#x27;ve tried my hand at explaining monads too. But what I wrote is way too long and too rough and too unedited and, for all three reasons, on Quora[1]. Still, might be worth a look.<p>[1]: <a href="http://www.quora.com/Functional-Programming/What-are-monads-and-why-are-they-useful/answer/Tikhon-Jelvis" rel="nofollow">http:&#x2F;&#x2F;www.quora.com&#x2F;Functional-Programming&#x2F;What-are-monads-...</a>
评论 #7798855 未加载
platz将近 11 年前
That wikipedia quote needs to be banished into the 7th circle of monad shame.
dukerutledge将近 11 年前
Meh, a ton of blow harding with little explanation.
评论 #7798541 未加载
评论 #7798857 未加载