define SciMax Toolbox defint

SciMax Toolbox >> define_variable

define_variable

Maxima Function

Calling Sequence

define_variable (name, default_value, mode)

Description

Introduces a global variable into the Maxima environment.

define_variable is useful in user-written packages, which are often translated or compiled.

define_variable carries out the following steps:

  1. mode_declare (name, mode) declares the mode of name to the translator. See for a list of the possible modes.

  2. If the variable is unbound, default_value is assigned to name.

  3. declare (name, special) declares it special.

  4. Associates name with a test function to ensure that name is only assigned values of the declared mode.

The value_check property can be assigned to any variable which has been defined via define_variable with a mode other than any. The value_check property is a lambda expression or the name of a function of one variable, which is called when an attempt is made to assign a value to the variable. The argument of the value_check function is the would-be assigned value.

define_variable evaluates default_value, and quotes name and mode. define_variable returns the current value of name, which is default_value if name was unbound before, and otherwise it is the previous value of name.

Examples:

foo is a Boolean variable, with the initial value true.

(%i1) define_variable (foo, true, boolean);
(%o1)                         true
(%i2) foo;
(%o2)                         true
(%i3) foo: false;
(%o3)                         false
(%i4) foo: %pi;
Error: foo was declared mode boolean, has value: %pi
 -- an error.  Quitting.  To debug this try debugmode(true);
(%i5) foo;
(%o5)                         false

bar is an integer variable, which must be prime.

(%i1) define_variable (bar, 2, integer);
(%o1)                           2
(%i2) qput (bar, prime_test, value_check);
(%o2)                      prime_test
(%i3) prime_test (y) := if not primep(y) then
                           error (y, "is not prime.");
(%o3) prime_test(y) := if not primep(y)
                                   then error(y, "is not prime.")
(%i4) bar: 1439;
(%o4)                         1439
(%i5) bar: 1440;
1440 is not prime.
#0: prime_test(y=1440)
 -- an error.  Quitting.  To debug this try debugmode(true);
(%i6) bar;
(%o6)                         1439

baz_quux is a variable which cannot be assigned a value. The mode any_check is like any, but any_check enables the value_check mechanism, and any does not.

(%i1) define_variable (baz_quux, 'baz_quux, any_check);
(%o1)                       baz_quux
(%i2) F: lambda ([y], if y # 'baz_quux then
                 error ("Cannot assign to `baz_quux'."));
(%o2) lambda([y], if y # 'baz_quux
                        then error(Cannot assign to `baz_quux'.))
(%i3) qput (baz_quux, ''F, value_check);
(%o3) lambda([y], if y # 'baz_quux
                        then error(Cannot assign to `baz_quux'.))
(%i4) baz_quux: 'baz_quux;
(%o4)                       baz_quux
(%i5) baz_quux: sqrt(2);
Cannot assign to `baz_quux'.
#0: lambda([y],if y # 'baz_quux then
                 error("Cannot assign to `baz_quux'."))(y=sqrt(2))
 -- an error.  Quitting.  To debug this try debugmode(true);
(%i6) baz_quux;
(%o6)                       baz_quux
define SciMax Toolbox defint