
theValueRef isn't a pointer to theValue that you can use to somehow change
theValue (which is immutable).
theValueRef is a reference to a "box" that contains a totally separate,
mutable value.
When you use newIORef to create theValueRef, it's *copying* theValue into
the box. When you mutate theValueRef, you're mutating the value inside the
box - theValue remains unchanged.
Cheers,
Adam
On 22 June 2012 11:30, Captain Freako
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
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe