[Haskell-cafe] generating random lists until some predicate holds

Nicholas O. Andrews nandrews at vt.edu
Sun Dec 28 16:39:31 EST 2008


Hi all,

What's the best way to implement the following Python code in Haskell?
It is purposefully written in a functional style (and as a result will
kill your recursion stack every other run).

# begin Python
from random import *

def genList ():
    return [randint(0,9) for x in range(10)]

def randWhile (predicate):
    result = genList ()
    if predicate(result):
        return result
    else:
        return randWhile (predicate)

def allEven (list):
    return reduce(lambda x,y: x and y, [x%2 == 0 for x in list])

print randWhile (allEven)
# End Python

Thanks!


More information about the Haskell-Cafe mailing list