36 ms·
I don't know, I read all these painful explanations of what monads are...combined with more stretches of the imagination to explain plausible uses for them. In
by eplanit 17y ago
I don't know, I read all these painful explanations of what monads are...combined with more stretches of the imagination to explain plausible uses for them. In the end, the authors achieve neither, with me.
I tend to write these off with "yeah, in my 20s and early 30s whilst in graduate Comp. Sci. schooling I enjoyed all this esoteric crap, too". :-)
- shrughes 17y agoThis is mostly because the monad abstraction is mostly useless in non-lazy languages. It's more useful in lazy languages because objects with monadic types can then be written self-referentially without trouble. It also hurts that Java and C++ syntax is extremely specialized towards avoiding the use of abstractions.
- fauigerzigerk 17y agoC++ may be overly complex but it is in an entirely different league than Java in terms of syntactical abstractions that you can implement on top of it.
- loup-vaillant 17y agoC++ has operator overloading, but surely that can't put in an "entirely different league"… Were you talking about template abominations like the ones in boost?
- fauigerzigerk 17y agoYes, in my view, languages supporting operator overloading, macros and first class functions are in a completely different league than languages that don't have those features when it comes to syntactical abstractions.
- loup-vaillant 17y agoWhere did you saw first class functions in C++??? It doesn't even have closures!
- fauigerzigerk 17y agoSorry, I meant to say function pointers. But hey, I'm far from a big supporter of C++. I know its deficiencies very well unfortunately.
- loup-vaillant 17y agoOK, then. I will just say that in my experience, C++ and Java don't have a huge difference in expressibility. What C++ may gain with syntactic tricks, it loses with manual memory management. Anyway, in the face of Haskell and Lisp, the differences between java an C++ don't look that great.
- shrughes 17y agoC++ and C++ development are hugely different from Java and Java development. If Java and C++ don't look very different to you, it's because you need to take another look at C++. About the only thing they have in common is their hideous verbosity. You can write C++ as if it were like Java, but you can write C++ as if it were like Haskell, too. Given the question, "What mainstream language is most like Haskell," the answer is C++. Unlike Java, C++ is actually capable of sidestepping its limitations.
- shrughes 17y agoThat's true. But there's still a syntactic cost and a cognitive cost to making useful abstractions that can be much less verbosely and more comfortably made in other languages. And I find myself going with less clean designs because of it.
- fauigerzigerk 17y agoI agree with that.
- _delirium 17y agoAlong similar lines, here's an investigation from a while back of why monads haven't caught on in Lisp: http://marijn.haverbeke.nl/monad.html http://marijn.haverbeke.nl/monad.html
- steveklabnik 17y agoIt's not really esoteric, though. You could have invented monads, after all. ( http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html http://blog.sigfpe.com/2006/08/you-could-have-invented-monad... ) In any case, monads are just what he said, a design pattern. I dislike the over-application of patterns as much as anyone does, but recognizing common abstractions is one of the parts of programming that separates the men from the boys. Monads just get a lot of attention because the pattern is super useful in certain situations, like pure functional programming.
- nostrademons 17y agoI think that "Overridable semicolon" captures most of what a monad is and why you'd want to use it. Imagine that the compiler provided a hook into the language so that you could pass around statements (the monad type), a means to convert an expression to a statement (return), and the semicolon operator that combines two statements (>>=). And then instead of implicitly letting you do anything in a statement, it required that you explicitly program the effects of that statement into the semicolon operator. The simplest possible semicolon is one that lets you do anything, including interact with the outside environment. That's the IO monad. But you could also have a semicolon that limits your side-effects to one particular area of memory - that's the State monad. Or you could have one that doesn't execute any of the following statements if an error occurs - that's an exception monad. Or you could have one that attempts to atomically execute a bunch of statements that may read & write from a block of memory, but if that memory is changed by another thread, the effects are all rolled back and tried again. That's the STM (software transactional memory) approach to concurrency. Or you could have one that sends a webpage to the user's browser with an HTML representation of the state, waits for a response, and then executes the rest of the statements. That's the approach taken by WASH, one of the Haskell web frameworks. It's a powerful tool, because it's basically rewriting the fundamentals of how the language works. And because of that, very few people ever have a reason to create a new monad type, which is why examples tend to fall flat. It also interacts heavily with other Haskell features, eg. you don't get this correspondence between monads and statements without Haskell's do-notation, and you can't use them to affect control flow without lazy evaluation, and they become very difficult to reason about without a static type system.