4 ms·
All essential applications of monads in Haskell are there to work around the limitations of purity. The I/O monad, ST monad, State monad, etc. Yes, I know List
by sltkr 5y ago
All essential applications of monads in Haskell are there to work around the limitations of purity. The I/O monad, ST monad, State monad, etc.
Yes, I know List and Maybe are also technically monads in Haskell. But you don't need to think of them as monads or even understand monads to do anything useful with them. Applying monad operators to lists and maybes is only occasionally useful.
In most impure functional languages (LISP, ML) you rarely model any data types as monads explicitly. As soon as you have side-effects, they lose their appeal.
- nightski 5y agoThis is just not true, you throw out some of the most useful monads :) Also ST and State have no side effects. The only monad in Haskell that has side effects is IO (unless you were to sneak in an unsafePerformIO but that is an entirely different discussion). So they aren't there to work around the limitations of purity, because they are pure.
- sltkr 5y ago> This is just not true, you throw out some of the most useful monads :) Namely? > Also ST and State have no side effects. I didn't say they do. I said they are useful only because Haskell doesn't allow side-effects. In ML or Lisp you'd just update a local variable instead.
- nightski 5y agoLists, Option, Either, Reader, Logging (non-effectful), STM etc... There is even a probability monad that I have found quite useful. ST & State are far more useful than updating a local variable. If you are going to choose the simplest use case for them ever and then say that is the only thing they can do it is not a productive discussion. The state monad is a lot like a context object that allows state to flow through function composition (for functions that have no knowledge of such state). For example you could compose a bunch of functions and have the results of each function call stored without changing any of the functions to know about the operation. It's a method of abstraction and encapsulation. It's not just monads that are useful either, it is the full hierarchy of Monoids, functors, applicatives, monads, etc...