The cond statement in lisp is really very handy, and I find it far more
useful than elseif, and indeed more useful than if-then-else which I use
only if I can see immediately that the decision process is very simple.
Lisp has all of these.
You would write
(cond ((= a b) a)
((< a b) (- b a))
(t b))
With the program attached, you could use this, in maxima:
infix("->", 40,40);
cond ( a=b ->a,
a<b -> b-a,
t -> b)
With case and cond, many programs manipulating expressions can be written
this way
F(expr):= cond( atom(expr) ->dosomething(expr),
T -> case(op(expr),
"+" ->dosomethingsumrelated(expr),
"*" ->
dosomethingproductrelated(expr),
..
[sin,cos,tan] ->
dosomethingtrigrelated(expr),
Otherwise -> error("what should I do
with", expr, "?")));
I hope you agree with me that this is an improvement over if then else if
then else..
The attached file implements both case and cond now.
The use of "->" as a separator is actually not imposed by the file, and you
can use any non-commuting binary or nary operators such as [ , , .] or : or
:: or make up something else like --|> .
RJF
_____
From: maxima-bounces at math.utexas.edu [mailto:maxima-bounces at math.utexas.edu]
On Behalf Of Alasdair McAndrew
Sent: Sunday, January 07, 2007 1:05 PM
To: maxima list
Subject: Re: [Maxima] Extending maxima language
I'd like the "if" statement amended to include "elseif"; for example:
if a=b then return(a) elseif a<b return(b-a) else return(b);
Currently there's no way of doing this without placing a second "if"
statement inside a "block" which is clumsy. If there was no restriction on
the number of elseif's then this would obviate the need for a case
statement, and also fit nicely into Maxima's current syntax.
-Alasdair
On 1/8/07, Andrej Vodopivec <andrej.vodopivec at gmail.com> wrote:
Since people are discussing a new case statement for maxima I propose
we add some more stuff at the same time.
Currently I can think of three things which I would like to have in
maxima. I think I would like to have 2) and 3) even more that 1).
1) cond statement (with whatever syntax)
2) assignments to lists:
something like [a,b,c] == [1,2,3] would be equivalent to a:1, b:2,
c:3. This could be done by extending : to work in this case or adding
a new operator == or similar.
3) unwind_protect:
I think Barton had an implementation which he posted to this list.
Andrej
_______________________________________________
Maxima mailing list
Maxima at math.utexas.edu
http://www.math.utexas.edu/mailman/listinfo/maxima
-------------- next part --------------
A non-text attachment was scrubbed...
Name: casemac.lisp
Type: application/octet-stream
Size: 2431 bytes
Desc: not available
Url : http://www.math.utexas.edu/pipermail/maxima/attachments/20070107/27612a8d/attachment-0001.obj