On 1/8/07, Stavros Macrakis <macrakis at alum.mit.edu> wrote:
> There has been a lot of discussion about the syntax of a Maxima case
> statement. Let's think about the semantics for a second.
I'm not convinced case is needed, since we have if--then--elseif--else.
> One is what kind of comparison to use. Is it syntactic equality (=)
> or semantic equality (equal)?
Not sure. I can see arguments both ways.
> case x of
> 0 => und,
> 1 => und,
> 1/(x*(x-1))
> endcase
endcase is a bit of cruft that I hope we'll avoid, if we end up
adopting case. (1) It's unneeded, since parentheses for grouping
work just fine. (2) There are no other foo-endfoo constructs
in Maxima at present, and for the sake of consistency I hope we're
not going to stick endif, endfor, enddo, or whatever all around.
I like the "=>" though, also the "of" before the list of alternatives.
> Are the alternatives evaluated, or (implicitly) quoted? Probably
> implicitly quoted -- though as a general rule I think implicit quoting
> gets you into trouble, it seems to make sense here. Any arguments the
> other way?
Well, implicit quoting can lead to bad surprises here, just like
implicit quoting in other contexts. Not sure I have a preference
one way or another, though.
> What if none of the clauses are true, and there is no otherwise clause?
Should return false, like if--then without else.
> ==Multiple matches==
>
> What about the (common) case where many alteratives are wanted in a branch, e.g.
>
> case op(x) of
> ["+", "-"] => ...
>
> or
>
> case sign(x) of
> [pos, pz, zero] => f(x),
> [neg, nz, zero] => -f(-x),
> [pnz, pn] => analyze(x)
> end case
>
> This sort of syntax is fine, unless you also want to allow compound
> objects (like lists and sets) in the selectors, e.g.
>
> case (list) of
> [] => ... /* handle empty list */
> [0] => ... /* special case */
> ...
Well, we could avoid sorting out all of this if we just stick to
if--then--elseif---else, that way programmers can be explicit about
what they want.
> What if more than one branch can match? Do we evaluate them in order?
Maybe any or all cases which match should be evaluated.
That would be fun, and also more mathematical.
FWIW
Robert