title: "Iterating until" with monads
body: <p>I have something like this, which is working:</p>
  
  <pre><code>main = do
    let p = something :: MyType
    p' &lt;- newP p
    p'' &lt;- newP p'
    p''' &lt;- newP p''
    print p'''
  </code></pre>
  
  <p>See the pattern? I want to repeat this until a certain condition is met, or until a maximum number of iterations passed.</p>
  
  <p>I found <code>Control.Monad.Loops.iterateUntil</code>, but I can't get it to work. Here's the code I have (<code>check</code> looks something like this <code>check :: MyType -&gt; Bool</code>):</p>
  
  <pre><code>main = do
    let p = something :: MyType
    let stopCondition (i, p) = i == 100 || check p
    let run (i, p) = (i + 1, newP p)
    p' &lt;- iterateUntil stopCondition (run (0, p))
    print p'
  </code></pre>
  
  <p>It doesn't even compile.</p>
  
  <p>Note that it's important that each time this thing runs, the result of <code>run</code> is fed to the next iteration (I'm not sure <code>iterateUntil</code> will do that).</p>