Primes Sieve of Eratosthenes


/* Primes Sieve of Eratosthenes
*  8-9-97 j.n.magee
*/

const MAX = 10
range T = 2..MAX

GEN      = GEN[2],
GEN[x:T] = ([x] -> if x<MAX then GEN[x+1]).

FILTER      = (in[p:T]->out[p]->FILTER[p]),
FILTER[p:T] = (in[x:T] -> 
		 if x%p!=0 then 
                    (pass[x]->FILTER[p]) 
                 else 
                    FILTER[p]
               ).

||PRIMES(N=3) = if N==1 then
		  (in:GEN || FILTER )
	        else
                  (PRIMES(N-1) /{mid/pass, prime/out} || FILTER/{mid/in})  
                 @{out,pass,prime}.

/* 
Compile SIEVE and Check Run, Safety or Draw to display primes
*/
||SIEVE = PRIMES(4)/{prime/out}.