Intro to Monads (new chapter of Learn You a Haskell for Great Good)
learnyouahaskell.com
learnyouahaskell.com
If i just waited for this, it could have spared me all this reading ;-)
Here's my take: invoking a function is like writing an email to it. It replies with a direct answer to your question and a PS at the bottom mentioning some interesting trivia. Sometimes you are more interested in the PS than the main message body. That's monads.
But teaching doesn't work that way, and more often than not presenting the 'long way' through a topic obscures the essential simplicity that actually helps people figure it out for themselves.
That burrito one is a good example; maybe interesting once you already know what's going on, but if you don't it's most likely just going to frighten you away.
Kinda sorta the point of having any sort of pedagogical aid, like a book, is that it gives you at least a prod in the right direction. Learning something just to need to unlearn it a few pages later wastes everyone's time.
Haskell, for example, is a pure functional language that doesn't have an explicit syntax for side effects or state. But a language without any side effects can not do anything more useful than heat up the CPU: you have to have input and output, which means allowing for side effects. The Monad was the most elegant solution presented for that issue.
In code a monad looks like a function which has more outputs then inputs, and the compiler takes care of channeling the additional input and output between functions adjacent to the monad in way that doesn't break the functional syntax. This differential between function inputs and outputs allows for some interesting use cases and thus the many varieties of monads. Also the compiler makes sure the order lazy evaluations dependent on monads happen in the right order so that you're not trying to parse file contents before you've read the file (because in a pure functional language, like cells in a spreadsheet, everything is intended to be evaluated all at once).
I think I may have to break down and read the O'Reilly book on Haskell (I've never looked for it, but I'm assuming one exists), because the tutorials all seem to use Haskell syntax nowadays and that keeps me from fully understanding many of the articles, even though I seem to be learning bits and pieces of Haskell each time I read one of these.
It is very good, and expressly isn't a monad tutorial in the usual style, although they do get covered eventually of course (but from memory without any extra emphasis or allusions to difficulty, magic, etc).
The authors spend an awful lot of time showing you the wrong way (i.e. non-idiomatic) to do something and revealing with a flourish the Haskell way. Really you ought to just skip the first half of each chapter and not waste time learning techniques that you have to unlearn later. But you can't, because of the way it's all so intertwined.
Latest peeve - sometimes they talk about wrapping and unwrapping values, which is fine. But sometimes they talk about "peeling". What's that? Is it just another word for unwrap? Is it some special Haskell term? I don't know, I'm a beginner!
On page 369 of my copy there's code that fails to compile and in the session output in the book there are actual error messages from the compiler - but the following paragraph of text talks about it as if it was giving the "correct" results...
The only reason to read RWH is that there's a dearth of Haskell books so you make the best of it - but compared to say Learning Python or Programming Python also from O'Reilly it's really quite poor.
There were a reasonable amount of typos and similar errors I seem to recall now actually, which is either surprising or perhaps revealing given that it was publicly reviewed.
That said, I still think it's a good book. I'm not that fussed by having to work through it; that's just how some books are constructed if they're not reference material, particularly when earlier chapters are pre-requisites. I take your point though.
As I said it has been a while, but I don't recall being perturbed by the non-idiomatic->idiom transition either. The numerous monad tutorials mentioned in other comments are a great example of how forcing a particular learning path on someone doesn't always work, but by the same token I believe it can be useful to gently show a common path from common naive code to idiom for example.
I'd still recommend it anyway, if just for the "real world" approach of the title.
A lot of people with Ph.D.s in Computer Science provided "comments" on the book, but did not necessarily review the whole book. Reviewing a book cover to cover takes time. I found a lot of academic mistakes in Real World Haskell, such as the fact they used the phrase "strong typing" which has pretty much been banned from academic programming language literature thanks to Benjamin Pierce's book Types and Programming Language, where Pierce says he reviewed tons of papers trying to decipher a common meaning for the phrase and couldn't. A better way to characterize a type system is by whether it is (a) sound (b) complete.
There were just too many examples in Real World Haskell where it could've been much better explained, especially considering the authors CVs.
That's at least the strategy that works best for me.
When they finally clicked for me... I felt like it was something so simple, and I was seriously overcomplicating it in my head, and wondered why they were so frustrating all along. I had a similar experience with NoSQL, actually.
RWH is an excellent book, you should certainly check it out.
You're on the right track about chaining and function composition. Monads remind me of Unix pipes, but with hooks for extensibility (such as Maybe's short circuiting), and with static checking of their guarantees. I also don't really care for Haskell, but I've implemented monads in Lua, OCaml, and Scheme.
No matter how I think about it, using return to put data into a monad just seems weird. Maybe I have to think of it as returning a monad? Which leaves bind. What do I have to imagine doing? Kidnapping the value and leaving the context behind?
Burritos and spaceships indeed.
Besides, a lot of languages have weird terminology.
Oh dear.