[Maxima] argument checking, was: About submatrix and its meaning
l.butler at ed.ac.uk
Sun Nov 22 03:30:37 CST 2009
On Sun, 22 Nov 2009, Robert Dodier wrote:
< On 11/21/09, Kostas Oikonomou <ko at research.att.com> wrote:
< > How about a question mark? E.g. "x?listp".
< > foo(P(x)) is too much like f(g(x)), function composition.
< Well, it turns out "?" interferes with the use of ? to indicate
< Lisp symbols. But "??" is OK.
< Here's a first attempt to use "??" expressions to indicate
< argument-checking predicates in function definitions.
< foo (x??integerp) := ifactors (x);
< foo (1234);
< => [[2, 1], [617, 1]]
< foo (a + 1);
< => foo: expected argument x to satisfy integerp test; found: a + 1
< bar (a ?? lambda ([x], is (abs (x) < 1))) := 1/(1 - a);
< bar (1/2);
< => 2
< bar (2);
< => bar: expected argument a to satisfy lambda([x], is(abs(x) < 1))
< test; found: 2
< What do you think? Patch attached.
Robert, I like this. I have a couple sggestions/requests. First,
your code signals an error and halts if an arg check fails. It
is preferable if the user could control this. Second, arg checking
is useful, from a user's point-of-view, for debugging, but one
may want to turn it off without changing the code.
I suggest the code be written so that an error handling structure
can be used; the structure itself could be as simple as a list like
[test_function, action_on_false, action_on_unknown], where
an omitted argument would be handled by defaults:
-[test_function,action_on_false] == [test_function, action_on_false,
-test_function == [test_function,error,error]
It's possible possible that action_on_* could just indicate a choice
between error/warning/nothing, or it might be more elaborate.
I must say that already your code does a better job issuing informative
error messages than most user code.
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.
More information about the Maxima