use(io). print_primes_upto_100:expr(void)=( is_prime:var(bool^101). clear_multiples(x:val(int)):expr(void)=( n:var(int). n:=2*x; while (n<=100) do ( is_prime[n]:=false; n:=n+x ) ). i:var(int). forseq i in 1 to 100 do is_prime[i]:=true; forseq i in 2 to 100 do if (is_prime[i]) then ( print(i); print(" is prime\n"); clear_multiples(i) ) ). print_primes_upto_100
A natural extension of this example is to write a function that takes as a parameter the upper limit on the numbers to process, for example with this declaration:
print_primes_upto(max:val(int)):expr(void)=(...This requires that the declaration of is_prime depends on the parameter max, i.e.
is_prime:var(bool^(max+1)).In general declarations like this are allowed, but it is crucial that the expression's value does not change while the variable remains in scope!
Also note that a bug in the current version of the compiler (see the release notes) makes it necessary to put eval() around the expression, as follows:
is_prime:var(bool^eval(max+1)).