5. Generator
A generator creates a series of numbers by applying an expression to the
value of an index. A generator is similar to a C style for
loop.
For this assignment, the index variable will always start at the lower
bound and continue until it is equal to the upper bound (the last value
of the index will be the upper bound). The index will always be
incremented by the integer value 1. In C, this would be:
for(int i = <start>; i <= <end>; ++i)
5.1. Generator Format
A generator statement will always follow the same format:
[<id> in <int_1>..<int_2> | <expr>];
id
is the identifier of the generator’s index.int_1
is an integer representing the lower bound of the generatorint_2
is an integer representing the upper bound of the generatorexpr
is an expression
For this assignment the value of the identifier variable <id>
:
is initialized to the value of
int_1
.is used to evaluate the expression.
is incremented by one.
stops when its value is greater than
int_2
.
For each value assumed by id
, expr
is used to generated the next
number in the series.
Examples of valid generators:
[i in 1..10 | i * i];
[i in 0..10 | 2 ^ i];
In this assignment white space is not important so the following is valid:
[i
in
1
..
10
|
i*i];
[i in 1..10|2^i];
Assertion: Whitespace is guaranteed to be a space, a tab, a carriage return, or a new line. (simple-whitespace)
Because identifiers need white space to separate each other the following is invalid:
[iin1..10|i*i];
[i in1..10|2^i];