True...here we go then:<br><br>import Data.IORef<br>import System.IO.Unsafe<br><br>mkNext :: (Num a) =&gt; IO (IO a)<br>mkNext = do<br>  ref &lt;- newIORef 0<br>  return (do modifyIORef ref (+1)<br>             readIORef ref)<br>
<br>next :: IO ()<br>next = do<br>  foo &lt;- mkNext<br>  a &lt;- sequence [foo,foo,foo]<br>  putStrLn $ show a<br><br><br>running next will print [1,2,3] which is the result of calling &#39;foo&#39; 3 times.<br><br>But technically then, mkNext is just an IO action which returns an IO action ;)<br>
and not a function which will return the next value each time it is called,<br>hence the need to extract the value from mkNext, then use it...<br><br>Cheers,<br>Tim<br><br><br><div class="gmail_quote">On Wed, Oct 21, 2009 at 1:30 PM, minh thu <span dir="ltr">&lt;<a href="mailto:noteed@gmail.com">noteed@gmail.com</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">2009/10/21 Tim Wawrzynczak &lt;<a href="mailto:inforichland@gmail.com">inforichland@gmail.com</a>&gt;<br>

<div class="im">&gt;<br>
&gt; Here&#39;s an example in the IO monad:<br>
&gt;<br>
&gt; import Data.IORef<br>
&gt; import System.IO.Unsafe<br>
&gt;<br>
&gt; counter = unsafePerformIO $ newIORef 0<br>
&gt;<br>
&gt; next = do<br>
&gt;   modifyIORef counter (+1)<br>
&gt;   readIORef counter<br>
&gt;<br>
&gt; Naturally, this uses unsafePerformIO, which as you know, is not kosher...<br>
<br>
</div>But you don&#39;t close around the Ref like in your schemy example.<br>
<br>
mkNext = do<br>
  ref &lt;- newIORef 0<br>
  return (do modifyIORef ref succ<br>
             readIORef ref)<br>
<br>
mimic your other code better.<br>
<br>
Cheers,<br>
<font color="#888888">Thu<br>
</font></blockquote></div><br>