6 ms·
What is a good online reference to learn about "Monads" for beginners? Someone posted a reference here on HN sometime ago but I lost it. Edit: The book was enj
by clark-kent 12y ago
What is a good online reference to learn about "Monads" for beginners? Someone posted a reference here on HN sometime ago but I lost it.
Edit:
The book was enjoyable and a delight to read, but I started struggling to keep up from Chapter 8. as it went into Monoids and Monads, I feel like I need to read other tutorials before coming back to it.
- TazeTSchnitzel 12y agoWell, Learn You a Haskell's explanation of Monads in Haskell seems alright.
- danem 12y agoThere was quite a long discussion about this on the Haskell subreddit this past week[1], and indeed this topic seems to always be something newcomers get caught up with. My advice is to use the language until you gain an intuition for them. Despite being rather simple, I found it was difficult to fully appreciate them without a decent amount of exposure. Really though, monads aren't what Haskell is all about, and don't let an incomplete understanding of them stop you from diving in. [1] http://www.reddit.com/r/haskell/comments/2vm0ed/is_there_a_good_succinct_metaphorfree_monad/ http://www.reddit.com/r/haskell/comments/2vm0ed/is_there_a_g...
- Retra 12y agoI recommend this to anybody who wants to learn about Haskell's important data types: https://wiki.haskell.org/Typeclassopedia https://wiki.haskell.org/Typeclassopedia
- bstamour 12y agoI found the typeclassopedia to be really helpful after I'd been programming in Haskell for a few years. It really helped tie all of the types together, but a part of me thinks that it might be a bit overkill for someone who's just dipping their toes in for the first time.
- Retra 12y agoI like that it builds up to monads by introducing functor and applicative first, as those are far easier stones to leap to.
- fleshweasel 12y agoI have seen a few monad tutorials and found this series far more comprehensible than others-- it starts with the real-world monads many programmers use unknowingly and works back to the definition. http://ericlippert.com/2013/02/21/monads-part-one/ http://ericlippert.com/2013/02/21/monads-part-one/
- cstrahan 12y agoI highly recommend watching Tom Stuart's talk "Refactoring Ruby with Monads": https://www.youtube.com/watch?v=J1jYlPtkrqQ https://www.youtube.com/watch?v=J1jYlPtkrqQ
- tomstuart 12y agoThanks! There’s a transcript at http://codon.com/refactoring-ruby-with-monads http://codon.com/refactoring-ruby-with-monads.
- mietek 12y agoThe original monad papers, by Philip Wadler. Accept no substitutes. http://homepages.inf.ed.ac.uk/wadler/topics/monads.html http://homepages.inf.ed.ac.uk/wadler/topics/monads.html http://homepages.inf.ed.ac.uk/wadler/ http://homepages.inf.ed.ac.uk/wadler/ Alternatively, if you really are allergic to LaTeX, or beards, the recently Oscared sigfpe’s “You Could Have Invented Monads! (And Maybe You Already Have.)” http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html http://blog.sigfpe.com/2006/08/you-could-have-invented-monad... There are no other monad tutorials. Just forget about them.
- codygman 12y agoI highly recommend "You could have invented Monads", it helped me a lot. I also recommend the Haskell Wikibook: http://en.wikibooks.org/wiki/Haskell/Understanding_monads http://en.wikibooks.org/wiki/Haskell/Understanding_monads
- nathell 12y agoYes there are. What made monads eventually "click" for me was the incredibly detailed, eight-part Mike Vanier's tutorial on monads in general and in Haskell in particular. First part: http://mvanier.livejournal.com/3917.html http://mvanier.livejournal.com/3917.html
- tensorproduct 12y agoYou Could Have Invented Monads is what really made monads "click" for me. There are better tutorials for how to use monads in the real world, but I've never read a better description of why you would want to, if you don't know what they are.
- Mikeb85 12y agoYou don't need to really understand monads to use them. Not at first anyway. They're basically just containers that allow you to use side effects, hold state, etc... https://acm.wustl.edu/functional/io-monad.jpg https://acm.wustl.edu/functional/io-monad.jpg
- wyager 12y ago>They're basically just containers that allow you to use side effects, hold state, etc... No, that is one use for Monads. A Monad is just something that has a bind (>>=) function and a return function.
- dllthomas 12y agoThe something has to be a single argument type constructor, and bind and return need to abide by some laws. I don't mean that as any sort of "gotcha", just some additional clarification - especially if people read your "just" too strong.
- vertex-four 12y ago> A Monad is just something that has a bind (>>=) function and a return function. You realise that this is the most useless description ever? While I'm sure it's right, it doesn't actually mean anything. Why's it a good abstraction for doing <X>, better than any other abstraction? If it abstracts over a gazillion different things, why not have special-purpose abstractions like other languages (and thus probably make the language a lot simpler to use)? Your description is on the same level as "programming languages are structured text".
- jdeisenberg 12y agoFirst, you are not alone. I also "hit the wall" when the discussion turned to functors, monoids, and monads. Now, let me be the token heretic here. I took an online Haskell course where the instructor said, "Now that you have learned about monads, please don't write yet another monad tutorial!" Perhaps the reason that there are so many tutorials about them is because the really are NOT easy to grasp.
- bcbrown 12y agoIf you don't mind abstract mathematics, the category theory lectures from https://www.youtube.com/playlist?list=PL8Ky8lYL8-Oh7awp0sqa82o7Ggt4AGhyf https://www.youtube.com/playlist?list=PL8Ky8lYL8-Oh7awp0sqa8... were what made monads finally click for me.
- platz 12y agoWhy do we need monads?: http://stackoverflow.com/a/28141248/176876 http://stackoverflow.com/a/28141248/176876
- akurilin 12y agoTheory is generally wasted on me until I bang my head against the code for a while. If you're like me, you'll probably find it easiest to try to use monads in real code over and over, mostly based on existing examples, and then it will finally click and you'll be able to go back and grok the underlying theory.
- exDM69 12y agoThey don't teach you number theory before they teach you addition and subtraction in primary school. Don't read the "Monad tutorials" and especially do not stop programming in Haskell until you "learn monads". Find one of the respected books, read it and do the exercises. You do not need to thoroughly understand monads in order to do some IO programming just like a violinist does not have to understand the physics of vibrating strings or you don't need to be an auto mechanic to drive to the grocery store. Write the "Guess the Number" game in Haskell using IO and you should understand the basics and that gets you far. Reading and writing to/from files and network sockets, etc works pretty much the same as you're used to from imperative programming languages. "Guess the number" contains just enough control flow (repetition, conditionals) to get you started. I've written tens of thousands of lines of Haskell code, including production code at work, and I still don't know what a Monoid is. I have only a faint theoretical understanding of monads, but I am fluent writing IO code and applying monads to other problems like parsing and error handling. You should only study monads when you're fluent doing some basic imperative programming in Haskell or if you are a math student and have a keen interest in category theory.
- tomjakubowski 12y ago> Reading and writing to/from files and network sockets, etc works pretty much the same as you're used to from imperative programming languages. I agree completely that it's not necessary (and maybe even detrimental) to learn monads to use Haskell's IO, but I think this claim is dubious. Not too many imperative languages have lazy IO and this feature can be a surprising hurdle for the Haskell beginner who is versed in other languages.
- dllthomas 12y ago"I still don't know what a Monoid is." That is surprising. They show up all over in fairly common libraries - for instance, Writer collects a monoidal value. The very simplest notion is "monoid is for things you can combine". Taken from math, a monoid is a set of things (a type) for which you have an operation (called mappend in Haskell) which you can combine associatively, where the set contains an element (mempty in Haskell) that is an identity with respect to that operation. Some common monoids are: Anything list-like: mappend appends two lists mempty is the empty list Integers over addition (newtype Sum) mappend adds two integers mempty is zero Tuple of monoids: mappend is componentwise mappend mempty is (mempty, mempty) Functions where the domain and range are the same: mappend is function composition mempty is id
- kaeluka 12y agoBoth monads and monoids: The only thing you need to learn about them is practice. Screw tutorials and books. Read LYAH's intro chapter once (you won't understand everything, but that's fine) and then write code.
- louthy 12y agoIf you want to start right at the concept level before jumping into code, this is a great intro to functors, applicatives, and monads. http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html http://adit.io/posts/2013-04-17-functors,_applicatives,_and_...
- sinkasapa 12y agoThe best explanation I ever had was, "It is just a type class." In other words, don't worry about some deep metaphor. A Monad will have a type constructor, bind and the badly named return. There are laws that say how they'll work. The intuitions can come later.
- domenzain 12y agoStephan Boyer teaches about them in a peculiarly pragmatic way. Haskell programmers often abstract application-specific computations into monads and use them as a sort of interface—a functional API. http://www.stephanboyer.com/post/9/monads-part-1-a-design-pattern http://www.stephanboyer.com/post/9/monads-part-1-a-design-pa...