[Haskell-cafe] Is unsafePerformIO safe here?

Matthew Brecknell haskell at brecknell.org
Sun Dec 7 22:00:28 EST 2008


John Ky said:
> Does that mean there is no place to store state while running the
> interpreter [...]?

If all you are doing is experimenting at the GHCi prompt, then maybe
this is what you are missing:

...> moo <- newTVarIO 1
...> :t moo
moo :: TVar Integer
...> atomically (readTVar moo)
1
...>

You can also perform "let"-bindings:

...> let incr v = readTVar v >>= writeTVar v . (+1)
...> :t incr
incr :: (Num a) => TVar a -> STM ()
...> atomically (incr moo)
...> atomically (readTVar moo)
2
...>

For the details, see the GHC reference.



More information about the Haskell-Cafe mailing list