Paul Rubin wrote: > I tried to code the Sieve of Erastosthenes with generators: > > def sieve_all(n = 100): > # yield all primes up to n > stream = iter(xrange(2, n)) > while True: > p = stream.next() > yield p > # filter out all multiples of p from stream > stream = (q for q in stream if q%p != 0) > > # print primes up to 100 > print list(sieve_all(100)) > > but it didn't work.
This is a known issue with the scope rules in loops which has bitten many (including myself; there is a long thread involving me and Jacek Generowitz debating this at death, you may find it if you google the newsgroup). I would be curious to know if your code would work the way you expect in Haskell (I know it would for 'for' loop, dunno about 'while' loops, I am completely ignorant in Haskell). Michele Simionato -- http://mail.python.org/mailman/listinfo/python-list