Maxima Operator
::=
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.
Examples
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).
(%i1) x: %pi; (%o1) %pi (%i2) y: 1234; (%o2) 1234 (%i3) z: 1729 * w; (%o3) 1729 w (%i4) printq1 (x) ::= block (print ("(1) x is equal to", x), '(print ("(2) x is equal to", x))); (%o4) printq1(x) ::= block(print("(1) x is equal to", x), '(print("(2) x is equal to", x))) (%i5) printq1 (y - z); (1) x is equal to y - z (2) x is equal to %pi (%o5) %pi
An ordinary function evaluates is 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 ''%
.
(%i1) x: %pi; (%o1) %pi (%i2) y: 1234; (%o2) 1234 (%i3) z: 1729 * w; (%o3) 1729 w (%i4) printe1 (x) := block (print ("(1) x is equal to", x), '(print ("(2) x is equal to", x))); (%o4) printe1(x) := block(print("(1) x is equal to", x), '(print("(2) x is equal to", x))) (%i5) printe1 (y - z); (1) x is equal to 1234 - 1729 w (%o5) print((2) x is equal to, x) (%i6) ''%; (2) x is equal to %pi (%o6) %pi
macroexpand
returns a macro expansion.
macroexpand (foo (x))
followed by ''%
is equivalent to foo (x)
when foo
is a macro function.
(%i1) x: %pi; (%o1) %pi (%i2) y: 1234; (%o2) 1234 (%i3) z: 1729 * w; (%o3) 1729 w (%i4) g (x) ::= buildq ([x], print ("x is equal to", x)); (%o4) g(x) ::= buildq([x], print("x is equal to", x)) (%i5) macroexpand (g (y - z)); (%o5) print(x is equal to, y - z) (%i6) ''%; x is equal to 1234 - 1729 w (%o6) 1234 - 1729 w (%i7) g (y - z); x is equal to 1234 - 1729 w (%o7) 1234 - 1729 w