[Haskell-cafe] Question on proper use of Data.IORef

Captain Freako capn.freako at gmail.com
Fri Jun 22 17:30:41 CEST 2012


Hi experts,


I fear I don't understand how to properly use *Data.IORef*.
I wrote the following code:


  1 -- Testing Data.IORef
  2 module Main where
  3
  4 import Data.IORef
  5
  6 bump :: IORef Int -> IO()
  7 bump theRef = do
  8     tmp <- readIORef theRef
  9     let tmp2 = tmp + 1
 10     writeIORef theRef tmp2
 11
 12 main = do
 13     let theValue = 1
 14     print theValue
 15     theValueRef <- newIORef theValue
 16     bump theValueRef
 17     return theValue


and got this, in ghci:


*Main> :load test2.hs
[1 of 1] Compiling Main             ( test2.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
1
*1*


I was expecting this:


*Main> :load test2.hs
[1 of 1] Compiling Main             ( test2.hs, interpreted )
Ok, modules loaded: Main.
*Main> main
1
*2*


Can anyone help me understand what I'm doing wrong?


Thanks!
-db
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120622/22432e0c/attachment.htm>


More information about the Haskell-Cafe mailing list