## Maxima – 45 – Operatori – operatori di assegnamento – 1 Continuo da qui, copio dal Reference Manual, PDF scaricabile da qui, sono a p.135.

`:`
Assignment operator.

When the left-hand side is a simple variable (not subscripted), `:` evaluates its right-hand side and associates that value with the left-hand side.

When the left-hand side is a subscripted element of a list, matrix, declared Maxima array, or Lisp array, the right-hand side is assigned to that element. The subscript must name an existing element; such objects cannot be extended by naming nonex-istent elements.

When the left-hand side is a subscripted element of an undeclared Maxima array, the right-hand side is assigned to that element, if it already exists, or a new element is allocated, if it does not already exist.

When the left-hand side is a list of simple and/or subscripted variables, the right-hand side must evaluate to a list, and the elements of the right-hand side are assigned to the elements of the left-hand side, in parallel.

See also `kill` and `remvalue`, which undo the association between the left-hand side and its value.

Assignment to a simple variable.

``````(%i1) a;
(%o1)                                  a
(%i2) a : 123;
(%o2)                                 123
(%i3) a;
(%o3)                                 123``````

Assignment to an element of a list.

``````(%i4) b : [1, 2, 3];
(%o4)                              [1, 2, 3]
(%i5) b : 456;
(%o5)                                 456
(%i6) b;
(%o6)                             [1, 2, 456]``````

Assignment creates an undeclared array.

``````(%i7) c : 789;
ARRSTORE: use_fast_arrays=false; allocate a new property hash table for \$C
(%o7)                                 789
(%i8) c;
(%o8)                                 789
(%i9) c;
(%o9)                                  c
(%i10) arrayinfo (c);
(%o10)                         [hashed, 1, ]
(%i11) listarray (c);
(%o11)                               ``````

Multiple assignment is carried out in parallel. The values of `a` and `b` are exchanged in this example.

``````(%i12) [a, b] : [33, 55];
(%o12)                             [33, 55]
(%i13) [a, b] : [b, a];
(%o13)                             [55, 33]
(%i14) a;
(%o14)                                55
(%i15) b;
(%o15)                                33``````

`::`
Assignment operator.

`::` is the same as `:` except that `::` evaluates its left-hand side as well as its right-hand side.

``````(%i16) x : 'foo;
(%o16)                                foo
(%i17) x :: 123;
(%o17)                                123
(%i18) foo;
(%o18)                                123
(%i19) x : '[a, b, c];
(%o19)                             [a, b, c]
(%i20) x :: [11, 22, 33];
(%o20)                           [11, 22, 33]
(%i21) a;
(%o21)                                11
(%i22) b;
(%o22)                                22
(%i23) c;
(%o23)                                33``````

`::=`
Macro function definition operator. `::= `defines a function (called a “macro” for historical reasons) which quotes its arguments, and the expression which it returns (called the “macro expansion”) is evaluated in the context from which the macro was called. A macro function is otherwise the same as an ordinary function.

`macroexpand` returns a macro expansion (without evaluating it). `macroexpand(foo (x))` followed by `''%` is equivalent to `foo(x)` when `foo` is a macro function.

`::=` puts the name of the new macro function onto the global list macros. `kill`, `remove`, and `remfunction` unbind macro function definitions and remove names from macros.

`fundef` or `dispfun` return a macro function definition or assign it to a label, respectively.

Macro functions commonly contain `buildq` and `splice` expressions to construct an expression, which is then evaluated.

A macro function quotes its arguments, so message (1) shows `y - z`, not the value of `y - z`. The macro expansion (the quoted expression ‘`(print ("(2) x is equal to", x)))` is evaluated in the context from which the macro was called, printing message (2).

``````(%i24) x: %pi\$

(%i25) y: 1234\$

(%i26) z: 1729 * w\$

(%i27) printq1 (x) ::= block (print ("(1) x is equal to", x),
'(print ("(2) x is equal to", x)))\$

(%i28) printq1 (y - z);
(1) x is equal to y - z
(2) x is equal to %pi
(%o28)                                %pi``````

Ci devo pensare su 😯; OK, richiede una pausa tipo “stai fermo un giro” al Gioco dell’Oca 😋

An ordinary function evaluates its arguments, so message (1) shows the value of `y - z`. The return value is not evaluated, so message (2) is not printed until the explicit evaluation `''%`.

``````(%i30) x: %pi\$

(%i31) y: 1234\$

(%i32) z: 1729 * w\$

(%i33) printe1 (x) := block (print ("(1) x is equal to", x),
'(print ("(2) x is equal to", x)))\$

(%i34) printe1 (y - z);
(1) x is equal to 1234 - 1729 w
(%o34)                    print((2) x is equal to, x)
(%i35) ''%;
(2) x is equal to %pi
(%o35)                                %pi``````

`macroexpand` returns a macro expansion.
``` macroexpand(foo(x))``` followed by `''%` is equivalent to `foo(x)` when `foo` is a macro function.

``````(%i36) x: %pi\$

(%i37) y: 1234\$

(%i38) z: 1729 * w\$

(%i39) g (x) ::= buildq ([x], print ("x is equal to", x))\$

(%i40) macroexpand (g (y - z));
(%o40)                    print(x is equal to, y - z)
(%i41) ''%;
x is equal to 1234 - 1729 w
(%o41)                           1234 - 1729 w
(%i42) g (y - z);
x is equal to 1234 - 1729 w
(%o42)                           1234 - 1729 w``````

Di nuovo 😯; anzi: pausa 😁

Posta un commento o usa questo indirizzo per il trackback.