I&#39;ll give it shot, but I&#39;m also learning Haskell, so take this with a grain of salt :)<div><div><br></div><div><div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;">randWhile :: ([Int]-&gt;Bool) -&gt; StdGen -&gt; [Int]</span></div>
<div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;">randWhile predicate &nbsp;= head . filter predicate . blocks 10 . randomRs (0,9)</span></div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;">&nbsp;&nbsp; &nbsp;where</span></div>
<div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;">&nbsp;&nbsp; &nbsp; &nbsp;blocks n xs = let (y,ys) = splitAt n xs in y : blocks n ys</span></div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;"><br>
</span></div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;">main = newStdGen &gt;&gt;= print . randWhile (all even)</span></div><div><br></div><div>or if you prefer non point free notation (point full?)</div>
<div><br></div><div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;">randWhile :: ([Int]-&gt;Bool) -&gt; StdGen -&gt; [Int]</span></div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;">randWhile predicate rndGen &nbsp;= head $ filter predicate $ blocks 10 $ randomRs (0,9) rndGen</span></div>
<div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;">&nbsp;&nbsp; &nbsp;where</span></div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;">&nbsp;&nbsp; &nbsp; &nbsp;blocks n xs = let (y,ys) = splitAt n xs in y : blocks n ys</span></div>
<div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;"><br></span></div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;">main = do</span></div>
<div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;">&nbsp;&nbsp;rndGen &lt;- newStdGen</span></div><div><span class="Apple-style-span" style="font-family: &#39;courier new&#39;, monospace;">&nbsp;&nbsp;print $ randWhile (all even) rndGen</span></div>
<div><br></div></div></div><div class="gmail_quote">2008/12/28 Luke Palmer <span dir="ltr">&lt;<a href="mailto:lrpalmer@gmail.com">lrpalmer@gmail.com</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="Ih2E3d">On Sun, Dec 28, 2008 at 2:39 PM, Nicholas O. Andrews <span dir="ltr">&lt;<a href="mailto:nandrews@vt.edu" target="_blank">nandrews@vt.edu</a>&gt;</span> wrote:<br></div><div class="gmail_quote"><div class="Ih2E3d">
<blockquote class="gmail_quote" style="border-left:1px solid rgb(204, 204, 204);margin:0pt 0pt 0pt 0.8ex;padding-left:1ex">
Hi all,<br>
<br>
What&#39;s the best way to implement the following Python code in Haskell?<br>
It is purposefully written in a functional style (and as a result will<br>
kill your recursion stack every other run).</blockquote></div><div>&nbsp;<br>Here&#39;s my solution, using MonadRandom (from Hackage).&nbsp; There may be more infinite-listy ways of doing it, but I wasn&#39;t able to make it come out clean.<br>

<br><font face="courier new,monospace">import Control.Monad.Random<br><br>many n = sequence . replicate n<br><br>untilM p m = do<br>&nbsp; x &lt;- m<br>&nbsp; if p x then return x else untilM p m<br><br>getList :: MonadRandom m =&gt; m [Int]<br>

getList = many 10 $ getRandomR (0,9)<br><br>main = print =&lt;&lt; evalRandIO (untilM (all even) getList)<br><br></font><br></div><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left:1px solid rgb(204, 204, 204);margin:0pt 0pt 0pt 0.8ex;padding-left:1ex">


# begin Python<br>
from random import *<br>
<br>
def genList ():<br>
 &nbsp; &nbsp;return [randint(0,9) for x in range(10)]<br>
<br>
def randWhile (predicate):<br>
 &nbsp; &nbsp;result = genList ()<br>
 &nbsp; &nbsp;if predicate(result):<br>
 &nbsp; &nbsp; &nbsp; &nbsp;return result<br>
 &nbsp; &nbsp;else:<br>
 &nbsp; &nbsp; &nbsp; &nbsp;return randWhile (predicate)<br>
<br>
def allEven (list):<br>
 &nbsp; &nbsp;return reduce(lambda x,y: x and y, [x%2 == 0 for x in list])<br>
<br>
print randWhile (allEven)<br>
# End Python<br>
<br>
Thanks!<br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">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>
</blockquote></div></div><br>
<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>