[Haskell-beginners] Global variables
michael at snoyman.com
Thu Jun 30 07:28:50 CEST 2011
On Thu, Jun 30, 2011 at 8:21 AM, Ertugrul Soeylemez <es at ertes.de> wrote:
> Christopher Done <chrisdone at googlemail.com> wrote:
>> > I tend to use Control.Monad.Reader for stateful stuff like this. It
>> > is found in the mtl package
>> > http://hackage.haskell.org/package/mtl-18.104.22.168
>> How would you use reader for a counter?
> Reader is a good choice for an interthread counter:
> MonadIO m => ReaderT (TVar Int) m
> I think, then people are talking about global variables in Haskell, what
> they really want is such a thing.
Though in this case, you can go with the cheaper IORef, since
(presumably) the only operation is going to be incrementing the
counter. Just be sure to use atomicModifyIORef.
More information about the Beginners