<div class="gmail_quote">2011/1/12 Neil Brown <span dir="ltr">&lt;<a href="mailto:nccb2@kent.ac.uk">nccb2@kent.ac.uk</a>&gt;</span><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5">On 11/01/11 23:19, Tim Baumgartner wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hi,<br>
<br>
I&#39;m having difficulties with this function I wrote:<br>
<br>
iterateR :: (MonadRandom m) =&gt; (a -&gt; m a) -&gt; a -&gt; m [a]<br>
iterateR g s = do<br>
  s&#39; &lt;- g s<br>
  return (s:) `ap` iterateR g s&#39;<br>
<br>
I&#39;m running the computation with evalRandIO and surprisingly the first call of main in ghci succeeds, but the second does not terminate. Reproducible.<br>
Any clues what I&#39;m doing wrong here?<br>
</blockquote>
<br></div></div>
If we unfold ap we get:<div class="im"><br>
<br>
iterateR g s = do<br>
  s&#39; &lt;- g s<br></div>
  f &lt;- return (s:)<br>
  x &lt;- iterateR g s&#39;<br>
  return (f x)<br>
<br>
What happens here depends on exactly how the monad is defined, but for many monads that will form an infinite loop that prevents a value being returned.  In the case of RandT from MonadRandom, it is not possible to execute the action after the iterateR call finishes without knowing the final state from the call, which requires evaluating the infinite loop of monadic actions.  Does that help?<br>
</blockquote></div><br>Yes, this helps definitely. So if I understand you right, the infinite loop was not entered immediately because of lazyness? That&#39;s funny somehow.<br><br>Thanks a lot<br>