3 ms·
Brushing aside "you can do anything in one language that you can in another," what do monads allow me to do better that I can only approach without them? I've
by a3n 11y ago
Brushing aside "you can do anything in one language that you can in another," what do monads allow me to do better that I can only approach without them?
I've tried reading (but not coding), and all I hear is sequence of operations, chaining, and similar. I know I'm missing something.
Basically I haven't seen or synthesized the tl;dr that makes me want to learn a language that has them.
- AnimalMuppet 11y agoIf you're in a pure functional language, monads are the way you do sequencing while still being purely functional. If you regard pure functional as the "better" (for reasons of, say, controlling the state space explosion of your code, or reasons of understandability and therefore maintainability), then monads are part of the necessary tooling to get you there (if your problems have any parts at all that are necessarily sequential). As far as other uses: The best explanation that I've seen is that they let you hide the parts that you need to do, but that aren't the real thing your doing. Logging, for instance. You can add logging to something, and just string functions together as if there were no logging going on. (Of course, if you were doing non-pure-functional programming, you could just add logging statements wherever you wanted, and string your functions together just the same...)
- TheCoelacanth 11y agoAll useful languages have monads. It's a pattern that is nearly impossible to avoid. Where Haskell is different than most languages is that it lets you write generic code that operates on any monad.
- AnimalMuppet 11y agoMeaning that any language that doesn't have monads, you're defining to not be useful? Or meaning that monad-the-pattern is everywhere, in every even barely useful language, even if there's no explicit monads in the language?
- TheCoelacanth 11y agoThe monad pattern is everywhere, most languages just don't explicitly recognize it. Any non-trivial program will include many different monads; they just won't be recognized as such in most languages. For instance in C#, there is a monad with bind=?? and return=no-op (similar in use to the Maybe monad in Haskell). There's another monad with bind=; and return=no-op (similar in use to the IO monad in Haskell). C# just doesn't explicitly recognize those as monads.
- ch 11y agoI like this comment because it helps illustrate the subtlety of monads as they apply to programming. I think mode C# programmers would likely think of ';' and '??' as syntax, and who is thinking of 'no-op' as anything!? But if you were in haskell this would not be syntax, but just functions on an instance of the Monad type class. Haskell could easily add some sugar to make these constructs syntax as well, but it would just be sugar -- and that is where the power, and confusion I think arises when most programmers think about monads.
- a3n 11y ago"It's a pattern." "All useful languages have monads ... nearly impossible to avoid." So here's a post on Python (hopefully useful) and monads, it appears to be implementing a pattern, although I don't think I would have ever stumbled across it naturally. http://www.valuedlessons.com/2008/01/monads-in-python-with-nice-syntax.html http://www.valuedlessons.com/2008/01/monads-in-python-with-n... And here's a library implementing monads and other stuff: https://pypi.python.org/pypi/PyMonad/ https://pypi.python.org/pypi/PyMonad/ It still looks like I'm going to have to try using them before I can figure out if it's worth trying to use them.
- jberryman 11y agoHaskell per se doesn't really have monads, except in the sense that there's an opaque `IO` type which is only useful for "doing IO" because it is an instance of `Monad` (and `Functor`), and in the sense that there's some very thin syntactic sugar for composing monadic types. So haskell-the-language has type classes (like `Monad` or `Functor` or `Show`), and more importantly strong static typing. The latter means you can create abstractions (like Monad) that are not brittle. You can check out this module: http://hackage.haskell.org/package/base-4.8.1.0/docs/Control-Monad.html http://hackage.haskell.org/package/base-4.8.1.0/docs/Control... You can see all the varied sorts of things which are "instances of Monad", then scroll beyond and see all these different useful operations which behave sensibly for all those different instances (and from which you can build your own more complex operations which work for any Monad, and which also behave sensibly).