Live data from Hacker News

Ask HN: What is a monad?

news.ycombinator.com

1–10 of 55 posts

Ask HN: What is a monad?

#1
I'd like to start digging into functional programming beyond what is available in C# and Javascript, and began by reading a little about Haskell. There are a lot of mentions about something called Monads, which are part of category theory.

Can someone provide an explanation that would make sense to an experienced developer in the OOP world?

Re: Ask HN: What is a monad?

#4
Monads are (quote) elephants: http://james-iry.blogspot.com/2007/10/monads-are-elephants-p...

Monads can be implemented in OO languages, like ruby: http://moonbase.rydia.net/mental/writings/programming/monads...

Tony Morris, on of the authors scalaz (which implements monads, among other things, in scala ): http://projects.tmorris.net/public/what-does-monad-mean/arti...

A one that was considered a very good explanation on the topic: http://blog.sigfpe.com/2006/08/you-could-have-invented-monad...

Re: Ask HN: What is a monad?

#6
A cheap and unexciting trick for passing a value through a couple of functions. (Monads are cool, but it's important to understand that there's nothing, nuttin', whatsoever fancy about them, so I like to call them a "cheap trick".)

This is a good intro: http://homepages.inf.ed.ac.uk/wadler/papers/marktoberdorf/ba...

Be sure to mentally replace "monad" with "warm fuzzy thing" when reading it and it should make sense.

Re: Ask HN: What is a monad?

#9
It's a design pattern for encapsulation, with similar goals to OO but different methods. Rather than telling you what a monad is, I'll just tell you the goal.

OO is designed to hide the dangerous parts of the world from you. The dangerous parts of the world (or at least some dangerous parts of the world) are encapsulated inside little black boxes with well defined interaction points. If you interact only through those little boxes, you should be safe. For instance, a logger object gives you 1 methods: writeLog (I'll ignore logging levels). The details of actually writing logs are not your business, and you (in principle) don't need to worry about them.

Monads are stricter. A monad puts you into a box and only gives you a few interaction points with the outside world. You can do whatever you want in a monad, but you are never allowed to leave the box. Inside the logger box, you are permitted only pure functions (functions with no side effects) and writeLog.

So basically, in an OO world, code using a logger might call os.unlink("/var/log/myprogram.log"). This would interact in a harmful way with your logging mechanisms. In a monadic world, from inside the logging monad, you are not given access to os.unlink.

Thus, f: a -> Loggable b in the monadic world (where Loggable b is an b living inside the box) is safer than f: (Logger, a) -> b in the OO world.

Post reply on HN