39.2.1 Ordinary functions-------------------------
To define a function in Maxima you use the
:= operator. E.g.
f(x) := sin(x)
defines a function
f. Anonmyous functions may also be created using
lambda. For example
lambda ([i, j], ...)
can be used instead of
f(i,j) := block (, ...); map (lambda ([i], i+1), l)
would return a list with 1 added to each term.
You may also define a function with a variable number of arguments,by having a final argument which is assigned to a list of the extraarguments:
(%i1) f ([u]) := u; (%o1) f([u]) := u (%i2) f (1, 2, 3, 4); (%o2) [1, 2, 3, 4] (%i3) f (a, b, [u]) := [a, b, u]; (%o3) f(a, b, [u]) := [a, b, u] (%i4) f (1, 2, 3, 4, 5, 6); (%o4) [1, 2, [3, 4, 5, 6]]
The right hand side of a function is an expression. Thus if youwant a sequence of expressions, you do
f(x) := (expr1, expr2, ...., exprn);
and the value of <exprn> is what is returned by the function.
If you wish to make a
return from some expression inside thefunction then you must use
block (, expr1, ..., if (a > 10) then return(a), ..., exprn)
is itself an expression, and so could take the place of the righthand side of a function definition. Here it may happen that the returnhappens earlier than the last expression.
 in the block, may contain a list of variables andvariable assignments, such as
[a: 3, b, c: ], which would cause thethree variables
c to not refer to their global values,but rather have these special values for as long as the code executesinside the
block, or inside functions called from inside the
block.This is called dynamic binding, since the variables last from the startof the block to the time it exits. Once you return from the
block,or throw out of it, the old values (if any) of the variables will berestored. It is certainly a good idea to protect your variables inthis way. Note that the assignments in the block variables, are donein parallel. This means, that if you had used
c: a in the above,the value of
c would have been the value of
a at the time you justentered the block, but before
a was bound. Thus doing something like
block ([a: a], expr1, ... a: a+3, ..., exprn)
will protect the external value of
a from being altered, but wouldlet you access what that value was. Thus the right hand side of theassignments, is evaluated in the entering context, before any bindingoccurs. Using just
block ([x], ... would cause the
x to have itselfas value, just as if it would have if you entered a fresh Maximasession.
The actual arguments to a function are treated in exactly same way asthe variables in a block. Thus in
f(x) := (expr1, ..., exprn);
we would have a similar context for evaluation of the expressions asif we had done
block ([x: 1], expr1, ..., exprn)
Inside functions, when the right hand side of a definition, may becomputed at runtime, it is useful to use
define and possibly
39.2.2 Array functions----------------------
An array function stores the function value the first time it is calledwith a given argument, and returns the stored value, withoutrecomputing it, when that same argument is given. Such a function isoften called a memoizing function.
Array function names are appended to the global list
arrays (notthe global list
arrayinfo returns the list ofarguments for which there are stored values, and
listarray returnsthe stored values.
fundef return the array functiondefinition.
arraymake constructs an array function call, analogous to
funmake for ordinary functions.
arrayapply applies an arrayfunction to its arguments, analogous to
apply for ordinary functions.There is nothing exactly analogous to
map for array functions,although
map(lambda([<x>], <a>[<x>]), <L>) or
makelist(<a>[<x>],<x>, <L>), where <L> is a list, are not too far off the mark.
remarray removes an array function definition (including anystored function values), analogous to
remfunction for ordinaryfunctions.
kill(<a>[<x>]) removes the value of the array function <a> storedfor the argument <x>; the next time <a> is called with argument <x>,the function value is recomputed. However, there is no way to removeall of the stored values at once, except for
remarray(<a>), which also remove the function definition.
There are also some inexact matches for
?? function to see them.
(%o1) true (%i2)