Next: Data Structures Up: Introduction Previous: Indexed names and

Statements: Assignment, Conditional, Loops

The Maple syntax for the assignment, if, for and while statements is taken from Algol 60. The assignment statement looks like

name := expr

where expr is any expression and name is a variable name. We want to mention here an evaluation problem that you will sooner or later run into. It arises because variables can be assigned expressions which contain symbols as well as numbers. Consider the assignment to earlier.


> p := x^2+4*x+4;

                                     2
                               p := x  + 4 x + 4

Here we assigned the name to a formula that explicitly contains the symbol . What happens now if we use the same name on both sides of the assignment statement?


> p := p+x;
                                     2
                               p := x  + 5 x + 4

Nothing spectacular. But let us see how the evaluation rules operate here. The name is assigned the result of evaluating and also simplifying the right hand side . In evaluating , the value of is the polynomial and the value of is just . The result of evaluation is therefore the expression . Next this is simplified to the final result . Now comes the big question. What happens if was not already assigned a value? Let's see what happens. Using instead of


> q := q+x;

                     Warning: Recursive definition of name

                                   q := q + x

Well, you may say, obviously the user forgot to assign a value first. Indeed that is probably the case, but let us continue and see what happens to Maple. Maple certainly allows to not have a value just like in the previous example.

The above statement resulted in a warning from Maple. The name is now assigned a formula which involves . What happens now if we try to evaluate ? We would have to evaluate . But to evaluate we have to evaluate again. Therein lies an infinite evaluation loop. On my system, if I try to evaluate , Maple dies with a rather spectacular crash


> q := q+x^2;
Segmentation fault

To recover from such a warning, simply unassign the variable , i.e. do q := 'q';. Note that Maple does not detect all recursive assignments because this would be too expensive to do in general.

In a conventional programming language, this problem of a recursive definition cannot arise because all variables must have values. If they haven't been assigned a value, this is really an error. Depending on the language, variables either get a default value, or they get whatever junk happens to be present in memory at the time, or in a few cases, the language attempts to detect this and issue an error.

The conditional statement in Maple has the following syntax.

if expr then statseq

[ elif expr then statseq ]*

[ else statseq ]

fi

where statseq is a sequence of statements separated by semi-colons, denotes an optional part, and * denotes a part which can be repeated zero or more times. A typical if statement would be


if x < 0 then -1 elif x = 0 then 0 else 1 fi

The for statement has two variations. The first one is

[ for name ] [ from expr ] [ by expr ] [ to expr ] [ while expr ]

do statseq od

Thus each of the for, from, by, to, while clauses can be omitted. If omitted, the default values are a dummy variable, 1, 1, and true respectively. A typical example of a for loop is


for i to 10 do print(i^2) od;

If one omits the for, from, by and to clauses we have a so called while loop.


i := 10^10+1;
while not isprime(i) do i := i + 2 od;
print(i);

Combining the for and while loops is sometimes nice, e.g. this example of searching for the first prime could be done as


for i from 10^10+1 by 2 while not isprime(i) do od;
print(i);

The above example illustrates that the value of the loop index can be accessed after the loop has terminated.

The second variation on the for loop is the so called for-in loop. It is really just a short hand for this loop which occurs very often


for i to nops(s) do f(op(i,s)) od;

where may be a sum, but in general, any Maple expression or data structure such as a list or set - see next section. One can code this as a for-in loop as follows


for i in s do f(i) od;

The syntax for the for-in loop is

[ for name ] [ in expr ] [ while expr ]

do statseq od


bondaren@thsun1.jinr.ru