global counters
Hal Daume III
hdaume@ISI.EDU
Wed, 19 Dec 2001 16:18:35 -0800 (PST)
Please, no tirade about banishing unsafePerformIO.
I've seen this done before I just don't remember how. I want to use a
state monad to count things, but don't want to monadify the thing I'm
using the counter in.
basically, i want a function getVar :: () -> String which returns a new
string every time. i tried this:
> curVarId = newSTRef (0 :: Integer)
>
> {-# NO-INLINE newVar -}
>
> newVar = \_ -> ('\0' : show x)
> where y = unsafePerformIO (stToIO curVarId)
> x = unsafePerformIO $ stToIO $
> do x <- readSTRef y
> writeSTRef y (x + 1)
> return x
and in ghci it works wonderfully, but when i actually compile, all i get
is "\NUL0".
what's the proper way to write this?
thanks
- hal