20 Dec
2001
20 Dec
'01
12:18 a.m.
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