[Haskell-cafe] Re: Monads aren't evil? I think they are.

Ertugrul Soeylemez es at ertes.de
Sun Jan 11 05:10:16 EST 2009

"Apfelmus, Heinrich" <apfelmus at quantentunnel.de> wrote:

> Ertugrul Soeylemez wrote:
> > Let me tell you that usually 90% of my code is monadic and there is
> > really nothing wrong with that.  I use especially State monads and
> > StateT transformers very often, because they are convenient and are
> > just a clean combinator frontend to what you would do manually
> > without them:  passing state.
> The insistence on avoiding monads by experienced Haskellers, in
> particular on avoiding the IO monad, is motivated by the quest for
> elegance.
> The IO and other monads make it easy to fall back to imperative
> programming patterns to "get the job done".  [...]

Often, the monadic solution _is_ the elegant solution.  Please don't
confuse monads with impure operations.  I use the monadic properties of
lists, often together with monad transformers, to find elegant
solutions.  As long as you're not abusing monads to program
imperatively, I think, they are an excellent and elegant structure.

I said that 90% of my code is monadic, not that 90% of it is in IO.  I
do use state monads where there is no more elegant solution than passing
state around.  It's simply that:  you have a structure, which you modify
continuously in a complex fashion, such as a neural network or an
automaton.  Monads are the way to go here, unless you want to do
research and find a better way to express this.

Personally I prefer this:

  somethingWithRandomsM :: (Monad m, Random a) => m a -> Something a

over these:

  somethingWithRandoms1 :: [a] -> Something a
  somethingWithRandoms2 :: RandomGen g => g -> Something a

Also I use monads a lot for displaying progress:

  lengthyComputation :: Monad m => (Progress -> m ()) -> m Result

> Consciously excluding monads and restricting the design space to pure
> functions is the basic tool of thought for finding such elegant
> abstractions. [...]

You don't need to exclude monads to restrict the design space to pure
functions.  Everything except IO and ST (and some related monads) is
pure.  As said, often monads _are_ the elegant solutions.  Just look at
parser monads.


nightmare = unsafePerformIO (getWrongWife >>= sex)

More information about the Haskell-Cafe mailing list