(*** Eratosthenes sieve by counting ***) use"streams" ; fun sieve s = case head s of NONE => cons( fn() => ( NONE , sieve( tail s ) ) ) | SOME h => let fun sweep s = itsweep s 1 and itsweep s n = cons( fn() => if n = h then ( NONE , sweep (tail s) ) else ( head s , itsweep (tail s) (n+1) ) ) in cons( fn() => ( SOME h , sieve( sweep (tail s) ) ) ) end ; fun prettys s = case head s of NONE => prettys( tail s ) | SOME n => cons( fn() => ( n , prettys( tail s ) ) ) ; fun prettyl [] = [] | prettyl ( NONE::l ) = prettyl( l ) | prettyl ( SOME n::l ) = n :: prettyl( l ) ; fun from n = cons( fn () => ( SOME n , from(n+1) ) ) ; val primes = prettys( sieve( from 2 ) ) ; take 22 primes ; take 78 ( sieve( from 2 ) ) ; prettyl it ;