The Assignment Statement
Assignments as Expressions
lhs := rhs;
The assignment statement lhs := rhs assigns to the lhs the value of rhs. The left-hand side of the assignment must be a name, indexed-name, function call, or expression sequence of these.
The assignment statement functions as follows.
The left-hand side is resolved to a name. (For more information, see the evaln function).
The right-hand side is evaluated as an expression.
The assignment is performed. The value of the assignment statement is the right-hand side.
If the left-hand side is a sequence of names, the right-hand side must evaluate to a sequence of expressions with the same number of components as the left-hand side. This is called a multiple assignment. The sequence of assignments are processed in the following order. First, all of the right-hand side expressions are evaluated. Then in a left to right pairwise manner the names on the left-hand side are evaluated and the matching value on the right-hand side is assigned to it.
Local variables can be declared with type assertions. The syntax is the same as that used for parameters. Whenever an assignment is made to a local variables with such a type assertion, the type of the right-hand side is checked after evaluation, but before the assignment is done. If the type of the right-hand side does not match, an assertion failure exception is raised.
Similarly, the left-hand side of any assignment can contain a type assertion that is checked before the assignment is carried out.
The setting of kernelopts(assertlevel) controls whether these type assertions are checked. Settings are described as follows.
0 - Turns off all assertion checking.
1 - Checks only assertions made using the ASSERT function.
2 - Checks ASSERT assertions, and assignment-type assertions.
An assignment statement can also be used as an expression, or embedded within most expressions, by enclosing it in parentheses. The result of an assignment expression is the right-hand side.
The assignment takes place only if and when the part of the expression containing it is evaluated. Except in the case of logical operators, which follow left-to-right McCarthy (short-circuit) evaluation rules, it is generally not possible to know the order in which subexpressions will be evaluated.
In addition to assigning an already computed value to a variable, it is possible to combine an operation and assignment into one using an assignment operator. These are:
Each of these performs the same operation that it would if the = were omitted, using the evaluated left and right hand sides of the assignment operator as its operands. The result is then assigned to the left hand side name.
The benefit of using an assignment operator is that the left hand side need only be resolved once. That is, the process of evaluating it to a name (which must be done before evaluating it to a value) is only done once. Thus, in an operator assignment such as A[veryBigExpression] += 1, the index veryBigExpression is only evaluated once. If the assignment were written as A[veryBigExpression] := A[veryBigExpression] + 1, then the index would be evaluated twice.
Like simple assignments (:=), an operator assignment can be used within an expression by enclosing it in parentheses. The result of such an embedded assignment is the computed value that was assigned to the left hand side.
The increment (+=) and decrement (-=) assignment operators each have an even shorter form that can be used when the right hand side is 1. The expression ++x is equivalent to x += 1, and --x is equivalent to x -= 1.
Unlike the longer forms, the short forms are expressions in their own right, and thus can be used within a larger expression without requiring extra parentheses (except where needed for disambiguation). The value of such an expression is the incremented or decremented value.
The short forms can also be written in postfix form (x++ and x--), in which case the effect on their argument, x, is the same, but the value of the expression is the original value of x.
If an operator assignment involving the same left hand side appears more than once in an expression, the order in which the assignments are carried out is undefined.
Special Semantics of the ,= Assignment Operator
In most cases, the operator assignment a ,= b is equivalent to a = a,b, forming an expression sequence from the values of a and b, and assigning it back to variable a.
In the special case where the value of the left hand side is a one dimensional Array, the Array is expanded and the right hand side appended to the Array in-place. If the right hand side is an expression sequence (e.g., a ,= b,c,d) each element of the sequence is appended separately.
If the left hand side is an Array with a hardware datatype, and a right hand side is a one dimensional Array, then the contents of that Array are appended to the left hand side.
If the left hand side is an Array with a datatype of integer (i.e., an array of bytes), and a right hand side is a string, the byte values of the characters in the string are individually appended to the left hand side.
If the left hand side is an object that implements a `,=` method, then that method is invoked to perform the operation. For example, the MutableSet object implements this method such that ms ,= x is equivalent to insert(ms,x) or ms union= MutableSet(x).
In cases other than those above, a right hand side is appended in its entirety as an element of the left hand side.
If any of the assignments described above are not possible (e.g. the right hand side is a symbol and the array has datatype float), an exception is raised.
i := 1;
a[i] := 2;
f(1) := 0;
(a,b) := (2,3);
(a,b) := (b,a);
g := proc(x) (x-1,x+1) end proc;
g:=procxx − 1,x+1end proc
(s,t) := g(10);
(j,c[j],f(j)) := 1, 2, 3;
Examples of typed assignments with assertion failures.
F := proc(x) local a::integer; a := x end proc:
Error, (in myassign) assertion failed in assignment, expected integer, got 3.4
b::float := [1,2,3,4,5];
Error, (in myassign) assertion failed in assignment, expected float, got [1, 2, 3, 4, 5]
Examples of assignments as, or within, expressions.
g((y := 2));
In the examples below, if a < b then the second assignment never takes place because the second operand of the or is not evaluated when the first is true.
(a, b) := (3, 4);
(smallest := a) <= b or (smallest := b) < a;
(a, b) := (3, 2);
Examples of operator assignments.
a := 1;
a += 17;
a mod= 5;
b := ++a;
c := a++;
Operator assignments are currently not supported in 2-D math input in the Standard Interface. They are supported in 1-D math input in the Standard Interface, as well as in all forms of input and output in the Command-line and Classic user interfaces.
Using the Variable Manager
Download Help Document
What kind of issue would you like to report? (Optional)