Maxima Function
catch (expr_1, ..., expr_n)
Evaluates expr_1, ..., expr_n one by one; if any
leads to the evaluation of an expression of the
form throw (arg)
, then the value of the catch
is the value of
throw (arg)
, and no further expressions are evaluated.
This "non-local return" thus goes through any depth of
nesting to the nearest enclosing catch
.
If there is no catch
enclosing a throw
, an error message is printed.
If the evaluation of the arguments does not lead to the evaluation of any throw
then the value of catch
is the value of expr_n.
(%i1) lambda ([x], if x < 0 then throw(x) else f(x))$ (%i2) g(l) := catch (map (''%, l))$ (%i3) g ([1, 2, 3, 7]); (%o3) [f(1), f(2), f(3), f(7)] (%i4) g ([1, 2, -3, 7]); (%o4) - 3
The function g
returns a list of f
of each element of l
if l
consists only of non-negative numbers; otherwise, g
"catches" the
first negative element of l
and "throws" it up.