The assignment Statement - Maple Programming Help

Home : Support : Online Help : Programming : Operations : Assignment : assignment

The assignment Statement

Description

 • lhs := rhs;
 • The assignment operator := assigns to the lhs the value of rhs. The left-hand side of the assignment operator 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.  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.

Examples

 > $i≔1$
 ${i}{≔}{1}$ (1)
 > $i$
 ${1}$ (2)
 > ${a}_{i}≔2$
 ${{a}}_{{i}}{≔}{2}$ (3)
 > ${a}_{i}$
 ${2}$ (4)
 > $f\left(1\right)≔0$
 ${f}{}\left({1}\right){≔}{0}$ (5)
 > $f\left(1\right)$
 ${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\left(10\right)$
 ${s}{,}{t}{≔}{9}{,}{11}$ (10)
 > $j,{c}_{j},f\left(j\right)≔1,2,3$
 ${j}{,}{{c}}_{{j}}{,}{f}{}\left({j}\right){≔}{1}{,}{2}{,}{3}$ (11)
 > $\mathrm{print}\left(c\right)$
 ${\mathrm{table}}\left(\left[{1}{=}{2}\right]\right)$ (12)
 > $f\left(1\right)$
 ${3}$ (13)

Examples of typed assignments with assertion failures.

 > $\mathrm{kernelopts}\left(\mathrm{assertlevel}=2\right):$
 > F := proc(x) local a::integer; a := x end proc:
 > $F\left(3.4\right)$
 > $b::\mathrm{float}≔\left[1,2,3,4,5\right]$