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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Haskell Concepts in One Sentence

341 点作者 crystalPalace大约 8 年前

25 条评论

scottmsul大约 8 年前
A monad is any data structure which implements bind. Bind is a higher-order function with two parameters - one is the data structure to be transformed, the other is a function which maps over elements in the data structure. However, unlike a normal map, each result of &quot;bind&quot; sits in its own version of the original data structure, which then have to be combined back into a single data structure. The way in which the data structures are combined is what makes each monad different.<p>For example, List is a monad. Suppose we had a List of Ints, such as [5,3,4]. If we were to run bind over this list, we would need a function that takes an Int and returns a List of something. We could use &quot;show&quot;, the function which converts Ints to Strings (a String is technically a List of Char. Since this is a List, we&#x27;re good). If we call bind using [5,3,4] and show, we get [&quot;5&quot;,&quot;3&quot;,&quot;4&quot;] which are then combined to &quot;534&quot;.<p>We can check with the interpreter (&gt;&gt;= is bind):<p>Prelude&gt; [5,3,4] &gt;&gt;= show<p>&quot;534&quot;
评论 #13959819 未加载
评论 #13959440 未加载
评论 #13960379 未加载
评论 #13960421 未加载
评论 #13959339 未加载
jnordwick大约 8 年前
My general problem with Haskell articles and such: they are written as if you already understand Haskell. They make total sense if you know the language, but if you are trying to learn are mostly useless if not more confusing. Or even worse, they devolve into dense academic prose even when writing introductory articles.<p>Sometimes they even use Haskell as if you already know it to try to explain it.<p>I&#x27;m still looking for a basic article that describes monads well. My first few languages were all functional too, so that isn&#x27;t the problem. I even still use APL derivatives.
评论 #13959487 未加载
评论 #13959916 未加载
评论 #13959260 未加载
评论 #13959295 未加载
评论 #13959511 未加载
评论 #13960035 未加载
评论 #13965962 未加载
评论 #13959222 未加载
oblio大约 8 年前
I&#x27;m still kind of having problems with monads. Funnily enough, I recently found an article explaining what monoids are: <a href="https:&#x2F;&#x2F;fsharpforfunandprofit.com&#x2F;posts&#x2F;monoids-without-tears&#x2F;" rel="nofollow">https:&#x2F;&#x2F;fsharpforfunandprofit.com&#x2F;posts&#x2F;monoids-without-tear...</a><p><pre><code> You start with a bunch of things, and some way of combining them two at a time. Rule 1 (Closure): The result of combining two things is always another one of the things. Rule 2 (Associativity): When combining more than two things, which pairwise combination you do first doesn&#x27;t matter. Rule 3 (Identity element): There is a special thing called &quot;zero&quot; such that when you combine any thing with &quot;zero&quot; you get the original thing back. With these rules in place, we can come back to the definition of a monoid. A &quot;monoid&quot; is just a system that obeys all three rules. Simple! </code></pre> Long explanation overall in the article, but based on 6th grade math. I understood it, and it stuck. Could someone extend the monad explanation from here? Maybe I&#x27;ll finally get it :)
评论 #13961659 未加载
评论 #13960926 未加载
评论 #13963330 未加载
评论 #13961720 未加载
评论 #13960671 未加载
评论 #13961164 未加载
aetherspawn大约 8 年前
Monads are not complex. A monad is a box. Any monad has two functions (plus others, imagine this is a minimal interface): bind and return.<p>You can put anything in the box. It&#x27;s TARDIS-like.<p>`return` replaces the thing that was in the box.<p>`bind` replaces the box.<p>Most monads have a number of functions that can only be executed over boxes. This is because the boxes have special metadata (for example, someone has been scribbling state underneath the box). The &#x27;get&#x27; function from the State monad just tells you to read the gibberish on the box instead of unpacking the box. The &#x27;set&#x27; function scribbles more stuff on the bottom of the box.<p>Useful monads then provide a function to put things into the box, work with the box and then throw the box away (or inspect it by itself). These are the functions called &#x27;runWhatever&#x27; for example &#x27;runState&#x27;, which lets you put an apple in the box, put a shipping label onto the box and then eventually separate the apple and the shipping label into each hand, throwing the box in the bin.<p>You can put anything in a box. Even more boxes. And when you&#x27;re inside the box, you can&#x27;t really tell how deep you are. If you&#x27;re in the bottom box you can&#x27;t actually see that you&#x27;re in 20 layers of boxes, and this is why Monads are so powerful for creating libraries and frameworks.
评论 #13960112 未加载
评论 #13960895 未加载
评论 #13960556 未加载
mpfundstein大约 8 年前
IMO the most accessible resource for learning functional concepts like Functors, Applicatives and Monads is the free online book: <a href="https:&#x2F;&#x2F;github.com&#x2F;MostlyAdequate&#x2F;mostly-adequate-guide" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;MostlyAdequate&#x2F;mostly-adequate-guide</a><p>It uses Javascript to explain everything from scratch. Pure-functions, currying, composition, functors, monads, applicatives and so on.<p>Its free, so check it out. Reading it and understanding the concepts completely changed my whole coding style in the last couple of month. I hope functional javascript becomes more mainstream and we will soon call this stuff &#x27;standard&#x27;. Its just too convenient to stack a Future on top of a Reader and get dependency injection + async function handeling without any boilerplate code.<p>P.S. Since ES6, javascript is wonderful. Functional code often really looks like lisp. Pity that we don&#x27;t have macros (yet, and actually there is a way (sweet.js).<p>P.P.S. If DrBoolean got you hooked. You might want to check Ramda and fantasy-land. The former is a more functional minded underscore&#x2F;lodash, the latter a specification (and community) for algebraic data structures in javascript to which a lot of new libraries adhere to.
评论 #14045614 未加载
评论 #13963506 未加载
LeanderK大约 8 年前
To everyone new to haskell and confused: Don&#x27;t judge haskell based on these definitions. They appear strange and crazy, but when you actually do stuff most of them turn out to be quite intuitive.<p>My advice is to ignore these things. Don&#x27;t read a million monad tutorials. Just play around and code something, you don&#x27;t have to understand the monad-definition before you can use the do-notation. Try to ignore them. After a while you get an intuition and then the definitions will make sense.
评论 #13961692 未加载
评论 #13961413 未加载
评论 #13961557 未加载
rawicki大约 8 年前
I see traces of OP getting to understand the joke &quot;A monad is just a monoid in the category of endofunctors, what&#x27;s the problem?&quot; :)
Cybiote大约 8 年前
This is a good start and might be useful to other beginners. From the vantage point of someone who already knows what the words mean, I can fill in the gaps in your definitions but unfortunately, a beginner might not be so able.<p>Here are some suggestions on how you might close some of those gaps a bit (I think allowing yourself to go over a sentence here and there should be ok).<p>You use fmap more than once without having defined it.<p>Currying: You need to define partial application.<p>Map and filter, I&#x27;d use collection instead of list. There is more nuance but that is good enough at this level.<p>Morphisms generalize the concept of function. They can be thought of as capturing structure (such as a set equipped with + and 0) preserving maps (which is something analogies try to do).<p>lazy evaluation could do with mentioning thunk, then you&#x27;d have to define what a thunk is, of course.<p>Fold: Folds are a lot more interesting and it&#x27;s not clear what you mean by your given definition. I suggest defining it in terms of structural recursion.<p>Category: It&#x27;s worth defining what objects mean to a category. As well, explicitly mentioning the laws of identity, composition and associativity rather than just the nebulous wording of configuration would be beneficial.<p>Functor: A more useful foundation is in thinking of functors as morphisms between categories.<p>Types: There is much more to types than this. Wrong in the correct direction is to think of types in terms of sets. Better yet, as propositions.<p>Type Classes: Since you mention parametric polymorphism, you should also mention ad-hoc polymorphism and how type classes and interfaces are examples.<p>algebraic data types: There&#x27;s a lot more to algebraic data types than this. After defining sum types and product types elsewhere, you can talk about why such types are called algebraic.<p>parametric polymorphism: what is a type variable?<p>monoid: Moniods also need an identity element, and giving examples is always useful: natural numbers: +,0 or strings: +,&quot;&quot;. One reason monoids are of special interest to computing is because they possess associativity, which is useful when parallelizing.
iamwil大约 8 年前
It&#x27;d help beginners if there was a sentence explaining what fmap is.
评论 #13960163 未加载
评论 #13959447 未加载
评论 #13959400 未加载
init0大约 8 年前
Jargon from the functional programming world in simple terms! <a href="http:&#x2F;&#x2F;git.io&#x2F;fp-jargons" rel="nofollow">http:&#x2F;&#x2F;git.io&#x2F;fp-jargons</a>
bonoetmalo大约 8 年前
We should probably add a sentence about not using fixed margins that will make the text body 1&#x2F;5th the width of my screen.
评论 #13959127 未加载
edem大约 8 年前
This is all useless for someone without haskell experience.
hota_mazi大约 8 年前
&gt; A monad is composed of three functions<p>Actually just two (bind and return). And three laws which are typically not captured in the type system and which must therefore be tested separately.
burticlies大约 8 年前
One simplistic but useful analogy I&#x27;ve found is that monads are a way to control program flow with functions. A monad is to functional programming what if&#x2F;else is to imperative.<p>It may not cover all the nitty gritty about what is and isn&#x27;t a monad. But it gets you a long way to understanding why you might use them.
strictfp大约 8 年前
A monad is a wrapper type designed to alter the semantics of chains of operations performed on its wrapped values.
babbeloski大约 8 年前
Question to JS people using Redux. Are the middlewares used to control side effects in actions considered monads? The action returns a description of the side effect, and the middleware handles the actual doing, leaving the programmer to focus on the pure aspects.
ncphillips大约 8 年前
These sentences are nice, but I feel like more definitions and some re-ordering could make the document as a whole more meaningful.<p>For example, Lift is defined way before Functor, and fmap is never defined so I had no idea what Lift was about despite that nice concise sentence.
csneeky大约 8 年前
What an excellent distillation!<p>It isn&#x27;t uncommon to see someone w̶i̶t̶h̶ ̶a̶ ̶f̶r̶a̶g̶i̶l̶e̶ ̶e̶g̶o̶ explain this stuff in a way that is needlessly complex and full of jargon that scares folks off.<p>Thanks for the great work here. We need more of this kind of thing in the FP world!
mbfg大约 8 年前
There is an adage that if you see a lot of advertising for a medicine (think hair regrowth formulas, or pills of reflux disease) you know that none of them are any good.<p>This page makes me wonder about monads in the same way.
评论 #13962074 未加载
jpt4大约 8 年前
Article appears to be in flux; definition of lazy evaluation changed.
评论 #13959126 未加载
dmead大约 8 年前
whats the third function for a monad? bind return and...?
评论 #13959250 未加载
评论 #13959234 未加载
评论 #13959218 未加载
leshow大约 8 年前
I&#x27;d modify the monoid line to include associativity and identity:<p>A monoid is a a type with an associative function and an identity function
a_imho大约 8 年前
The problem when starting out with Haskell is that you can&#x27;t google type errors.
bogomipz大约 8 年前
This is brilliant, thanks for sharing.
nickpsecurity大约 8 年前
Hmm. They certainly have a naming and explanation problem in Haskell land. Some impressions from a few of these better explanations.<p>&quot;A monad is composed of three functions and encodes control flow which allows pure functions to be strung together.&quot;<p>Gibberish compared to claim that monads just execute functions in a specified order. Aka an imperative function or procedure by one definition I&#x27;ve seen a lot. Of course, that monad definition might be wrong, too.<p>&quot;A recursive function is a function that calls itself inside its own definition.&quot;<p>That&#x27;s a recursive definition lol. Must have been a joke.<p>&quot;A monad transformer allows you to stack more than one monad for use in a function.&quot;<p>We&#x27;ve had composition of procedures for a long time. Perhaps Haskell could&#x27;ve called it a MonadComposer?<p>&quot;Lift is an operation on a functor that uses fmap to operate on the data contained in the functor.&quot;<p>fmap-apply() or something like that?<p>&quot;Optics(lens and prisms) allow you to get and set data in a data type.&quot;<p>Getters and setters. My early OOP&#x2F;C++ books are finally more intuitive than something.<p>&quot;Map applies a function to every element of a list.&quot;<p>foreach(function(), list)<p>&quot;A predicate is a function that returns true or false.&quot;<p>A boolean function.<p>&quot;Filter applies a predicate to a list and returns only elements which return true.&quot;<p>Syntactic sugar for a foreach(function, list()) where the function on each member is an If (Conditional) is TRUE Then AddElementToNewListThatsReturned(). Yeah, even the description of imperative version is getting long. This might be a productivity boost.<p>&quot;A morphism is a transformation from any object to any other object.&quot;<p>A cast from one object to another? Or one with an actual conversion function and&#x2F;or check? The functional name seems more accurate, though.<p>&quot;Algebraic data types are a method to describe the structure of types.&quot;<p>Ahh, they&#x27;re just structs. Wait, what is a type exactly? And therefore what is an abstract... oh darn...<p>&quot;Free monads allow the transformation of functors to monads.&quot;<p>A functor is an object that can be fmaped over. We covered map. Maybe the same. A monad is either an ordering of functions or something composed of three functions and encodes control flow composed of pure functions. Free monads are apparently an unknown thing that can transform objects that can be fmaped over into something composed of three functions with encoded control flow of composed, pure functions. I heard a lot of good Masters and Ph.D. proposals before this one. This is good, though. Especially quantifying the unknown aspects with a lot of NSF-funded R&amp;D.<p>&quot;A lambda is an unnamed function.&quot;<p>&quot;Types are an inherent characteristic of every Haskell expression.&quot;<p>&quot;Currying uses partial application to return a function until all arguments are filled.&quot;<p>&quot;A category is a collection of objects, morphisms, and the configuration of the morphisms.&quot;<p>Ok, I just have fun with that one. Author did good on a lot of them. I&#x27;m just going to leave these here as quotes to add to Free Monads in... The Advanced Course: Haskell in Two or More Sentences. They provide anywhere from no information at all to the uninitiated to extra confusion inspiring taking or avoiding Haskell courses. :)
评论 #13959865 未加载
评论 #13959432 未加载
评论 #13959335 未加载
评论 #13959848 未加载
评论 #13959990 未加载