News for hackers. Stuff that matters.
Re:Learn You a Haskell for Great Good (2008) by clark-kent on Wednesday February 18, 2015 @10:38PM (#9072293)
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.

Re:Learn You a Haskell for Great Good (2008) by exDM69 on Thursday February 19, 2015 @03:40AM (#9073041)
They 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.

Re:Learn You a Haskell for Great Good (2008) by dllthomas on Thursday February 19, 2015 @11:48AM (#9075014)
"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
Re:Learn You a Haskell for Great Good (2008) by tomjakubowski on Thursday February 19, 2015 @10:05AM (#9074337)
> 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.

Re:Learn You a Haskell for Great Good (2008) by danem on Wednesday February 18, 2015 @10:46PM (#9072309)
There 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_g...

Re:Learn You a Haskell for Great Good (2008) by mietek on Wednesday February 18, 2015 @11:20PM (#9072372)
The 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/

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-monad...

There are no other monad tutorials. Just forget about them.

Re:Learn You a Haskell for Great Good (2008) by codygman on Thursday February 19, 2015 @12:05AM (#9072486)
I 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

Re:Learn You a Haskell for Great Good (2008) by nathell on Thursday February 19, 2015 @04:51AM (#9073238)
Yes 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
Re:Learn You a Haskell for Great Good (2008) by tensorproduct on Thursday February 19, 2015 @06:01AM (#9073435)
You 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.
Re:Learn You a Haskell for Great Good (2008) by cstrahan on Wednesday February 18, 2015 @11:19PM (#9072369)
I highly recommend watching Tom Stuart's talk "Refactoring Ruby with Monads":

https://www.youtube.com/watch?v=J1jYlPtkrqQ

Re:Learn You a Haskell for Great Good (2008) by tomstuart on Thursday February 19, 2015 @08:07AM (#9073789)
Thanks! There’s a transcript at http://codon.com/refactoring-ruby-with-monads.
Re:Learn You a Haskell for Great Good (2008) by fleshweasel on Wednesday February 18, 2015 @10:52PM (#9072319)
I 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/
Re:Learn You a Haskell for Great Good (2008) by Retra on Wednesday February 18, 2015 @10:52PM (#9072318)
I recommend this to anybody who wants to learn about Haskell's important data types:

https://wiki.haskell.org/Typeclassopedia

Re:Learn You a Haskell for Great Good (2008) by bstamour on Thursday February 19, 2015 @09:32AM (#9074128)
I 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.
Re:Learn You a Haskell for Great Good (2008) by Retra on Thursday February 19, 2015 @05:24PM (#9077546)
I like that it builds up to monads by introducing functor and applicative first, as those are far easier stones to leap to.
Re:Learn You a Haskell for Great Good (2008) by Mikeb85 on Wednesday February 18, 2015 @11:48PM (#9072434)
You 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
Re:Learn You a Haskell for Great Good (2008) by acjohnson55 on Thursday February 19, 2015 @10:24AM (#9074453)
I'm going to make an attempt to succinctly explain monads:

A monadic data type is a container that can be mapped and flattened.

- It's a container, because it stores has an inner type, which refers to the data it contains. For example, think of the type parameter that describes the elements of a list.

- It is "mappable" in the sense that its contained type can be converted to another type by providing an operation from the first type to the second type. Imagine converting a list of strings to a list of integers, where each integer is the length of the corresponding string.

- It can be "flattened" in the sense that if its inner type is an extra nesting of the container type, it can be flattened to a just one level of the container type. For example, a list of lists of integers can be flattened to just a list of integers by concatenating the sublists.

- It seems kind of trivial to say, but there has to be a way to construct a container given one of the elements it's supposed to contain.

There are also a couple of rule these operations have to fulfill. They're kind of abstract when stated on their own, but make intuitive sense when you consider examples.

Pretty simple, huh? What's cool about it is that you can assign all sorts of metadata to the container type and semantics to the flatten operation, and you get a nice representation of a pipeline of operations, where each one depends on the all the previous steps. This is basically just imperative programming -- each step depends on the current context and can optionally mutate that context, affecting subsequent steps.

You might ask, "but isn't imperative programming what we're trying not to do?" In the most general sense with mutable state everywhere, yes. But monads give you a way to describe imperative logic in very tightly controlled contexts.

Re:Learn You a Haskell for Great Good (2008) by wyager on Wednesday February 18, 2015 @11:57PM (#9072459)
>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.

Re:Learn You a Haskell for Great Good (2008) by vertex-four on Thursday February 19, 2015 @06:45AM (#9073552)
> 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".

Re:Learn You a Haskell for Great Good (2008) by thedufer on Thursday February 19, 2015 @07:26AM (#9073673)
The point, I think, is that the original description leaves out the easiest monads, which makes learning about them a lot more difficult. See: List, Maybe, etc.
Re:Learn You a Haskell for Great Good (2008) by the_af on Thursday February 19, 2015 @08:30AM (#9073866)
Fully agreed that those are the easiest monads, far easier than IO, state and side-effects. But the post is still unhelpful, because how do List and Maybe relate to this:

> A Monad is just something that has a bind (>>=) function and a return function.

?

(I know how they relate, but the above assertion is not a particularly helpful description of List and Maybe)

Re:Learn You a Haskell for Great Good (2008) by wyager on Thursday February 19, 2015 @07:02PM (#9078058)
>but the above assertion is not a particularly helpful description of List and Maybe

Good. If my description of a Monad was a good description of List or Maybe, then I described Monads wrong.

Your complaint is like saying "Your description of a shape was not a helpful description of a dodecahedron." Indeed, a dodecahedron is a very specific instance of a shape.

Re:Learn You a Haskell for Great Good (2008) by the_af on Thursday February 19, 2015 @07:16PM (#9078120)
Exactly. However, note I wasn't disagreeing with you; I was disagreeing with user thedufer that "the point" was that "the original description leaves out the easiest monads, which makes learning about them a lot more difficult. See: List, Maybe, etc." (That's why I replied to him and not to you)

List and Maybe are indeed the easiest monads, but thinking about them in terms of (>>=) and return doesn't give a good intuition about monads, Lists or Maybe. So I think that wasn't "the point".

"The point" was actually that state and IO aren't the only monads, and that the definition is way more general than that. List and Maybe being the easiest monads is irrelevant.

Re:Learn You a Haskell for Great Good (2008) by wyager on Thursday February 19, 2015 @10:41PM (#9078784)
Gotcha. Then I am in absolute agreement.
Re:Learn You a Haskell for Great Good (2008) by wyager on Thursday February 19, 2015 @11:08AM (#9074751)
>You realise that this is the most useless description ever?

Which is perhaps why many people have a hard time understanding it. That is, however, the true definition.

>Why's it a good abstraction for doing <X>

That depends on <X>. I can't answer that question unless you tell me what <X> is.

> 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)?

Why would a special-purpose abstraction be easier to use? If you only understand monads in terms of state, you can just use them that way.

Re:Learn You a Haskell for Great Good (2008) by vertex-four on Thursday February 19, 2015 @01:30PM (#9075843)
> That depends on <X>. I can't answer that question unless you tell me what <X> is.

Isn't that kind of the problem? I have no idea why it's a good abstraction for anything or what sorts of things it's a good abstraction for. Is it a good abstraction for adding numbers together? God knows, nobody can explain it.

> Why would a special-purpose abstraction be easier to use?

Because it says what it does on the tin, rather than being buried under several abstraction layers. My for loop is a lot more obvious than your "lifting functions into the list monad" or whatever it is you do with them, and is quite obviously doing an entirely different thing from, say, dealing with the contents of a Maybe, or storing state, or executing IO operations.

We can play with abstractions upon abstractions all day, but unless they actually mean something solid, how are they useful in explaining or solving a problem?

Re:Learn You a Haskell for Great Good (2008) by wyager on Thursday February 19, 2015 @02:40PM (#9076430)
>Isn't that kind of the problem?

Maybe. Leaving things in the abstract can certainly be confusing sometimes.

>I have no idea why it's a good abstraction for anything or what sorts of things it's a good abstraction for.

I think once you understand Monads for what they are (i.e. accept that it's just something with bind and return), you start seeing where it makes sense to use Monads. Until then, you can just use whatever concrete Monad implementations you're aware of. It seems like you know perfectly well that there exist Monad implementations for Lists, for example.

> Because it says what it does on the tin, rather than being buried under several abstraction layers.

IO, List, and most other Monads do what they say on the tin, even if you're not aware that they're Monads.

You don't have to know if something is a Monad or not to use it. For example, I've been using Lists my entire programming career without it occurring to me that I could use them with bind to model nondeterminism or the ZipList Monad.

>but unless they actually mean something solid, how are they useful in explaining or solving a problem?

They are useful because they are examples of http://en.wikipedia.org/wiki/Algebraic_structure .

You can use the definition of an algebraic structure to prove things about all instances of that structure.

Monads are, in fact, quite useful even in their most general definition.

Re:Learn You a Haskell for Great Good (2008) by dllthomas on Thursday February 19, 2015 @08:59PM (#9078492)
ZipList isn't a monad, is it?
Re:Learn You a Haskell for Great Good (2008) by wyager on Thursday February 19, 2015 @10:49PM (#9078801)
You're right. I think I mis-remembered reading the Applicative instance for ZipList.
Re:Learn You a Haskell for Great Good (2008) by dllthomas on Thursday February 19, 2015 @12:04AM (#9072484)
The 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.

Re:Learn You a Haskell for Great Good (2008) by tjradcliffe on Thursday February 19, 2015 @11:50AM (#9075041)
New abstractions are generally better introduced to new audiences via concrete examples that can later be generalized rather than formal abstractions that float high above our heads.

The GP's explanation of the concrete features (with simple examples!) is one of the most useful explanations of monads I've seen. It isn't complete, but as a gateway to a complete explanation it is extremely good.

Re:Learn You a Haskell for Great Good (2008) by louthy on Thursday February 19, 2015 @08:29AM (#9073864)
If 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_...

Re:Learn You a Haskell for Great Good (2008) by jdeisenberg on Wednesday February 18, 2015 @11:58PM (#9072461)
First, 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.
Re:Learn You a Haskell for Great Good (2008) by domenzain on Thursday February 19, 2015 @12:21PM (#9075297)
Stephan 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-pa...

Re:Learn You a Haskell for Great Good (2008) by akurilin on Thursday February 19, 2015 @02:41AM (#9072905)
Theory 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.
Re:Learn You a Haskell for Great Good (2008) by bcbrown on Thursday February 19, 2015 @01:09AM (#9072685)
If you don't mind abstract mathematics, the category theory lectures from https://www.youtube.com/playlist?list=PL8Ky8lYL8-Oh7awp0sqa8... were what made monads finally click for me.
Re:Learn You a Haskell for Great Good (2008) by sinkasapa on Thursday February 19, 2015 @12:19PM (#9075267)
The 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.
Re:Learn You a Haskell for Great Good (2008) by kaeluka on Thursday February 19, 2015 @03:49AM (#9073065)
Both 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.
Re:Learn You a Haskell for Great Good (2008) by platz on Thursday February 19, 2015 @01:50AM (#9072789)
Re:Learn You a Haskell for Great Good (2008) by TazeTSchnitzel on Wednesday February 18, 2015 @10:41PM (#9072299)
Well, Learn You a Haskell's explanation of Monads in Haskell seems alright.