program printprimes;
This program prints out a list of prime numbers
-- which includes all primesless than a parameter
-- value which is specified as input data.
read(n); read input parameter
primes := { }; - set of primes output so far
p := 2; initial value to test
Loop to test successive values
while p < n loop loop as long as p less than n
ifnot (exists t in primes | p mod t = 0) then
print(p); no divisors, it's a prime
primes with:= p; add it to set of primes
endif;
p := p + 1; move to next test value
endloop;
end printprimes;
identifier := expression;
a := 3 + 4.0; INVALID!
b := float(3) + 4.0; b = 7.0
c := 3 + fix(4.0); c = 7
abc := "hello";
abc(3..4) := "xyz"; abc = "hexyzo"
abc(4..) := "m"; abc = "hexm"
Atoms are typically used as domain elements for maps
(later)
A procedure may be defined using a procedure definition,
or by means of the lambda construction:
a := om; a is now undefined
that is, if expr1 evaluates to om,
then x receives the value of expr2
t := [1,9,"abc","def"];
x := t(3); x = "abc"
t(4) := 0; t = [1,9,"abc",0]
The general form of this abbreviation is:
abc := [1,2,3];
cde := abc;
abc(2) := 0;
cde still = [1,2,3]
q := [ ];
q with:= 5; enqueue 5: q = [5]
q with:= 7; enqueue 7: q = [5,7]
e fromb q; dequeue: e
= 5, q = [7]
e fromb q; dequeue: e
= 7, q = [ ]
e fromb q; dequeue: e
= om, q = [ ] (queue empty)
s := {5,2,8};
a := s with 7; s = {5,2,8},
a = {5,2,7,8}
s with:= 6;
s = {5,6,2,8}
s with:= 5;
s = {5,6,2,8}
s less:= 5;
s = {6,2,8}
s less:= 0;
s = {6,2,8}
a := {[1,2], [1,3], [2,4], [5,5], [2,7], [2,8]};
c := a(1); c = om
d := a{1}; d = {2,3}
e := a{5}; e = {5}
f := a(5); f = 5
g := a{7}; g = { }
Binary Map Operators
Unary Map Operators
a := {[1,2], [1,3], [2,2], [2,4], [3,6], [3,7]};
b := domain a;
b = {1,2,3}
c := range a;
c = {2,3,4,6,7}
a lessf:= 1;
removes [1,2] and [1,3]
a(2) := om;
removes [2,2] and [2,4]
a{3} := { };
removes [3,6] and [3,7]
For example
Combining maps and atoms provides a way of dealing
with pointers and dynamic data structures:
case exprIf expr is equal to one of expr1, ..., the corresponding block is executed. A more general form is:
when expr1 => block1
when expr2 => block2
.
.
otherwise => blocke
end case;
case expr
when expr11,expr12,... => block1
when expr21,expr22,... => block2
.
.
otherwise => blocke
end case;
Boolean ProceduresThe equality and inequality comparisons may be used to compare values of any type for exact identity, including testing for equality with om.
even(i) Operand is even
odd(i) Operand is odd
is_integer(v) Operand is integer type
is_real(v) Operand is real type
is_tuple(v) Operand is tuple type
is_set(v) Operand is set type
is_map(v) Operand is map (set of pairs)
is_string(v) Operand is string
is_atom(v) Operand is atom
is_boolean(v) Operand is boolean
is_procedure(v) Operand is procedure
Binary Boolean Operators
and Logical conjunction of two boolean values (sequential)
or Logical inclusive disjunction (sequential)
Unary Boolean Operators
not Logical negation
type Yields the string "BOOLEAN"
for x in s loop
print(x);
prints elements of s
end loop;
for x in [1,10,50] loop
...
end loop;
for [number,root] in sqrt loop
-- Several variables
...
for root=sqrt(number) loop ...
-- Alternative form
for y=t(i) loop iterate
through tuple t,
-- assigning i and y
for c=s(i) loop iterate
through string s,
-- assigning i and character c
while test loop loop while test succeeds
...
end loop;until test loop loop until test succeeds
...
end loop;loop indefinite loop
...
end loop;
The exit statement may optionally
contain an expression:
In this way, an entire loop may be used as a single
expression, whose value is determined by the expression as evaluated when
the exit statement is executed:
x := for s in set loop
statement1; ... ;
if ... then exit expression; end if;
statements; ... ;
end loop; -- x is om if exit is never executed
A set former is a special form of a loop which computes
a set value with an iteration. The form is:
Examples:
Tuple formers:
exists iterator | testreturns true or false, and binds the iteration variable to either om (when false) or a value that satisfies the test;
forall iterator | testreturns true or false, and binds the iteration variable to either om (when true) or a value that does not satisfies the test.
Examples:
s := {1,2,10,20};
t := [1,2,10,20];if exists x in s | x > 3
then ... will be executed with x = 20 or 10
else ... will not be executed
end if;if exists x in t | x > 3
then ... will be executed with x = 10
else ... will not be executed
end if;if forall x in s | x < 30
then ... will be executed with x = om
else ... will not be executed
end if;if exists x in t | x > 30
then ... will not be executed
else ... will be executed with x = om
end if;if forall x in t | x < 10
then ... will not be executed
else ... will be executed with x = 10
end if;
Can be formed from any binary operator by appending a slash / to the name of the operator.
bop/ exprs -- exprs must be a set or tuple
expre bop/ exprs -- exprs must be a set or tuple
bop/ exprs means e1 bop e2 bop e3 ...Examples:
result is om if tuple or set is empty
expre bop/ exprs means expre bop e1 bop e2 ...
result is value of expre if tuple or set is empty+/ t sum of values in tuple
0+/ t same, but 0 rather than om for [ ]
*/ [a in s | 3 in a] -- inters. sets that contain 3
" "+/ t builds string from tuple of chars
case i
when 1,3,5 => print(i);
when 2,4,7 => print(i+1);
when 0,6,9 => null; do nothing in these cases
otherwise => print("no good");
end case;
stlc fileThe compiled units will be stored in the library file setl2.lib under the working directory.
To execute a program that has been successfully compiled, execute the command
where prog_name is the name of the program
from the program statement (and not the name of the file
containing the source file for the program).
See the manual pages for more information on the stll,
stlc,
and stlx commands.