Maxima Function
block ([v_1, ..., v_m], expr_1, ..., expr_n)
block(expr_1,...,expr_n)
block
evaluates expr_1, ..., expr_n in sequence
and returns the value of the last expression evaluated.
The sequence can be modified by the go
, throw
, and return
functions.
The last expression is expr_n unless return
or an expression containing throw
is evaluated.
Some variables v_1, ..., v_m can be declared local to the block;
these are distinguished from global variables of the same names.
If no variables are declared local then the list may be omitted.
Within the block,
any variable other than v_1, ..., v_m is a global variable.
block
saves the current values of the variables v_1, ..., v_m (if any)
upon entry to the block,
then unbinds the variables so that they evaluate to themselves.
The local variables may be bound to arbitrary values within the block but when the
block is exited the saved values are restored,
and the values assigned within the block are lost.
block
may appear within another block
.
Local variables are established each time a new block
is evaluated.
Local variables appear to be global to any enclosed blocks.
If a variable is non-local in a block,
its value is the value most recently assigned by an enclosing block, if any,
otherwise, it is the value of the variable in the global environment.
This policy may coincide with the usual understanding of "dynamic scope".
If it is desired to save and restore other local properties
besides value
, for example array
(except for complete arrays),
function
, dependencies
, atvalue
, matchdeclare
, atomgrad
, constant
, and
nonscalar
then the function local
should be used inside of the block
with arguments being the names of the variables.
The value of the block is the value of the last statement or the
value of the argument to the function return
which may be used to exit
explicitly from the block. The function go
may be used to transfer
control to the statement of the block that is tagged with the argument
to go
. To tag a statement, precede it by an atomic argument as
another statement in the block. For example:
block ([x], x:1, loop, x: x+1, ..., go(loop), ...)
. The argument to go
must
be the name of a tag appearing within the block. One cannot use go
to
transfer to a tag in a block other than the one containing the go
.
Blocks typically appear on the right side of a function definition but can be used in other places as well.