theValueRef isn't a pointer to theValue that you can use to somehow change theValue (which is immutable).<div>theValueRef is a reference to a "box" that contains a totally separate, mutable value.<div><br></div>
<div>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.</div><div><br></div><div>
Cheers,</div><div>Adam<br><div><br><div class="gmail_quote">On 22 June 2012 11:30, Captain Freako <span dir="ltr"><<a href="mailto:capn.freako@gmail.com" target="_blank">capn.freako@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<p>Hi experts,</p>
<p><br>I fear I don't understand how to properly use <em>Data.IORef</em>.<br>I wrote the following code:</p>
<p><br><font face="courier new,monospace"> 1 -- Testing Data.IORef<br> 2 module Main where<br> 3 <br> 4 import Data.IORef<br> 5 <br> 6 bump :: IORef Int -> IO()<br> 7 bump theRef = do<br> 8 tmp <- readIORef theRef<br>
9 let tmp2 = tmp + 1<br> 10 writeIORef theRef tmp2<br> 11 <br> 12 main = do<br> 13 let theValue = 1<br> 14 print theValue <br> 15 theValueRef <- newIORef theValue<br> 16 bump theValueRef<br>
17 return theValue</font></p>
<p><br>and got this, in ghci:</p>
<p><br><font face="courier new,monospace">*Main> :load test2.hs<br>[1 of 1] Compiling Main ( test2.hs, interpreted )<br>Ok, modules loaded: Main.<br>*Main> main<br>1<br><strong>1</strong></font></p>
<p><br>I was expecting this:</p>
<p><br><font face="courier new,monospace">*Main> :load test2.hs<br>[1 of 1] Compiling Main ( test2.hs, interpreted )<br>Ok, modules loaded: Main.<br>*Main> main<br>1<br><strong>2</strong></font></p>
<p><br>Can anyone help me understand what I'm doing wrong?</p>
<p><br>Thanks!<br>-db</p>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br></div></div></div>