 := - Maple Help

The Assignment Statement Description

 • 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.
 1 The left-hand side is resolved to a name. (For more information, see the evaln function).
 2 The right-hand side is evaluated as an expression.
 3 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. Assignments as Expressions

 • 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. Operator Assignments

 • 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:

 += ++ -= -- = .= /= mod= ^= ,= ||= and= or= xor= implies= union= intersect= minus=

 • 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 or a Vector, it is expanded and the right hand side appended to it 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 a one dimensional Array or a Vector with a hardware datatype, and an element of the right hand side is a one dimensional Array or a Vector, then the contents of that Array or Vector are appended to the left hand side.
 If the left hand side is a one dimensional Array or a Vector with a datatype of integer (i.e., an array of bytes), and an element of the right hand side is a string, the byte values of the characters in the string are individually appended to the left hand side. Such an Array or Vector can be converted back to a string using the String constructor.
 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. Examples

 > i := 1;
 ${i}{≔}{1}$ (1)
 > i;
 ${1}$ (2)
 > a[i] := 2;
 ${{a}}_{{i}}{≔}{2}$ (3)
 > a[i];
 ${2}$ (4)
 > f(1) := 0;
 ${f}{}\left({1}\right){≔}{0}$ (5)
 > f(1);
 ${0}$ (6)
 > (a,b) := (2,3);
 ${a}{,}{b}{≔}{2}{,}{3}$ (7)
 > (a,b) := (b,a);
 ${a}{,}{b}{≔}{3}{,}{2}$ (8)
 > g := proc(x) (x-1,x+1) end proc;
 ${g}{≔}{\mathbf{proc}}\left({x}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{x}{-}{1}{,}{x}{+}{1}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (9)
 > (s,t) := g(10);
 ${s}{,}{t}{≔}{9}{,}{11}$ (10)
 > (j,c[j],f(j)) := 1, 2, 3;
 ${j}{,}{{c}}_{{j}}{,}{f}{}\left({j}\right){≔}{1}{,}{2}{,}{3}$ (11)
 > print(c);
 ${table}{}\left(\left[{1}{=}{2}\right]\right)$ (12)
 > f(1);
 ${3}$ (13)

Examples of typed assignments with assertion failures.

 > kernelopts(assertlevel=2):
 > F := proc(x) local a::integer; a := x end proc:
 > F(3.4);
 > b::float := [1,2,3,4,5];

Examples of assignments as, or within, expressions.

 > g((y := 2));
 ${1}{,}{3}$ (14)
 > y;
 ${2}$ (15)

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);
 ${a}{,}{b}{≔}{3}{,}{4}$ (16)
 > (smallest := a) <= b or (smallest := b) < a;
 ${\mathrm{true}}$ (17)
 > smallest;
 ${3}$ (18)
 > (a, b) := (3, 2);
 ${a}{,}{b}{≔}{3}{,}{2}$ (19)
 > (smallest := a) <= b or (smallest := b) < a;
 ${\mathrm{true}}$ (20)
 > smallest;
 ${2}$ (21)

Examples of operator assignments.

 > a := 1;
 ${a}{≔}{1}$ (22)
 > a += 17;
 ${18}$ (23)
 > a mod= 5;
 ${3}$ (24)
 > b := ++a;
 ${b}{≔}{4}$ (25)
 > a, b;
 ${4}{,}{4}$ (26)
 > c := a++;
 ${c}{≔}{4}$ (27)
 > [a, c];
 $\left[{5}{,}{4}\right]$ (28) Compatibility

 • Assignments as expressions and 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 user interface.