Hacker News new | ask | show | jobs
by noloblo 1221 days ago
What problems does haskell create How do Monad transformers solve it @jeremyjh
2 comments

The main problems are immutability and inability perform I/O outside the IO monad. To address immutability you have Monads like Reader, Writer, State which can give you a similar experience that you would have with mutable data and/or global variables. For example with the state monad you can have code like:

  runState do
    value <- get
    put counter+1

The problem comes when you also want to do IO with that state; if your code is in the IO monad it has no access to the State monad and can't call those methods. There are ways to work around that but monad transformers give you a StateT which can be applied to any base monad, including IO, giving you the capability of both.
Wel IO is State, cf. https://hackage.haskell.org/package/transformers-0.6.1.0/doc...

You set up your pipeline in terms of monads and then when you want to execute it you run it in IO. You can generalize IO to MonadIO or define an alias to some transformer pinned at IO. It adds a Computational overhead. Transformers are a solution to monad composition which isn’t naturally possible.

Yes and they are essential to working in serious, modern Haskell. You could never be an intermediate without know how to use them.
Would it be possible to give an example of monad composition that shows both state and Io on one computation unit?
Is there an example of StateT applied to IO? That's not too complex for python#++ devs?
None. Transformers are a solution to the composability of monads.