[Haskell-beginners] Global variables

Ertugrul Soeylemez es at ertes.de
Thu Jun 30 20:19:44 CEST 2011

Yitzchak Gale <gale at sefer.org> wrote:

> > I tend to use Control.Monad.State for stateful stuff like this. It
> > is found in the mtl package
> > http://hackage.haskell.org/package/mtl-
> I agree that this is a good approach.
> If the only state you need is this, it might be easier just to add the
> counter as an additional parameter to your function, and return its
> incremented value as part of your return type.  For example, you can
> return a tuple (v, n) where v is the value you were returning before
> and n is the updated counter.
> If you also need other kinds of state, or if adding the counter
> complicates the types throughout your program, then the State monad is
> the way to go.

I agree that this is a good approach for this particular case, if the
counter is really attached to the given computation and is not
interesting elsewhere.

However for state in general there is an approach, which is often
forgotten:  implicit configurations.  This allows you to pull state
around without mentioning it explicitly in any way, somewhat related to
the ImplicitParams extension, but saner and with a sound theoretical
foundation.  The advantage is that the state is encapsulated in a type
class and thus you can have parallel state threads, invisibly pulling
around state attached to certain type constructors.

The paper "Functional Pearl: Implicit configurations" by Oleg Kiselyov
and Chung-chieh Shan introduces you to this abstraction.


nightmare = unsafePerformIO (getWrongWife >>= sex)

More information about the Beginners mailing list