[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Axiom-developer] RE: [Axiom-math] if-then-else: what is wrong?
From: |
Bill Page |
Subject: |
[Axiom-developer] RE: [Axiom-math] if-then-else: what is wrong? |
Date: |
Thu, 19 Jan 2006 11:46:03 -0500 |
On January 19, 2006 5:53 AM Fabio Stumbo wrote:
>
> I am having some problem with the if-then-else syntax in the
> .input files.
>
> Problem 1: I refer to the axiom book which ships with the program
> (version 2005/09). At page 204 it says that the usual rules
> for piling are suspended in conditional expressions and it gives
> 5 different ways which should be equivalent,
> ...
> Well, I tried all of them, creating each time a blank r.input
> file with inside only
>
> i := 2
>
> followed by one of the "if...".
>
> The result is that the first two are ok, where as the other
> three give some syntax error (different each time).
>
> So: what is happening? Is the book wrong?!?!
Worse than that: **both** :(
You have found bugs in both the documentation and in the
implementation!
There seems to be a bug in the Axiom interpreter that does not
treat the pile syntax correctly when the if statement is the
outer most block. So for example, if you try the third and
fourth cases shown in the Axiom book, but nest them inside
another block like this:
for i in 2..2 repeat
if i > 0 then output("positive")
else output("nonpositive")
for i in 2..2 repeat
if i > 0
then output("positive")
else output("nonpositive")
the nested if statements are both parsed and executed properly.
But as you reported both of these
if i > 0 then output("positive")
else output("nonpositive")
if i > 0
then output("positive")
else output("nonpositive")
given syntax errors when they are the outer most block. There
should be no difference.
On the other hand the last example given in the book:
if i > 0
then output("positive")
else output("nonpositive")
**is wrong** and it is also (correctly) reported as a syntax
error even if we write it nested inside another block:
for i in 2..2 repeat
if i > 0
then output("positive")
else output("nonpositive")
Further the statement that the "pile syntax is suspended" in the
book is both wrong and misleading. There are no cases in Axiom
when the pile syntax in suspended. I think the author of that part
of the book was confused.
>
> Problem 2: again with if then else, in a complex function I
> noticed a wrong result (the function itself was ok... ;-)
> Simplifying, you can produce the same error with something like:
>
> test: (INT,INT) -> List(INT,INT)
> test(a,b) ==
> x := 0; y := 0
> if (a rem b = 0) and b < 0 then
> x := 1 ; y := 1
> [x,y]
>
> Then, test(4,-2) returns correctly [1,1], but test(4,-3) or test(4,2)
> returns wrongly [0,1].
The problem is that you are mixing the two methods of writing blocks.
The use of the ';' separator in pile syntax might be confusing since
it is expected to be used with ( ) to form a block. Your statement:
if (a rem b = 0) and b < 0 then
x := 1 ; y := 1
is interpreted by Axiom as if you had written either:
if (a rem b = 0) and b < 0 then
x := 1
y := 1
or
if (a rem b = 0) and b < 0 then (x := 1;) y := 1
So you should write:
test: (INT,INT) -> List(INT)
test(a,b) ==
x := 0; y := 0
if (a rem b = 0) and b < 0 then
x := 1
y := 1
[x,y]
-------
Note: List only takes one argument. In Axiom lists are homogeneous
(all elements of the list must be of the same type).
> The same happens if I write
>
> if (a rem b = 0) and b < 0 then x := 1 ; y := 1
>
> Why?
The correct alternative way to write this would be:
if (a rem b = 0) and b < 0 then (x := 1 ; y := 1)
If you are not using the pile syntax, then you must group
statements using parenthesis. In the Axiom interpreter these
parentheses are treated the same way as brackets the { } in
some other languages.
>
> Another small question. Is there a way to obtain on line the
> syntax of a command in a more extensive way? I mean, for
> example, that writing
>
> (3) -> )di op extendedEuclidean
>
> I get
>
> (3) ->
> There are 2 exposed functions called extendedEuclidean :
> [1] (D,D,D) -> Union(Record(coef1: D,coef2: D),"failed") from D
> if D has EUCDOM
> [2] (D,D) -> Record(coef1: D,coef2: D,generator: D) from
> D if D has
> EUCDOM
>
> which is not too useful. Moreover, how can I just ask about, say,
> the second function extendedEuclidean?
>
> If I wanted to ask about the syntax of if-then-else, I would
> like to write
> something like
> )? if
> or
> )ap if
>
> but nothing gives clues on how to use it.
>
I think you should try Axiom's hyperdoc browser. (Unfortunately it
is only currently available in the linux version of Axiom.) Hyperdoc
provides a lot of useful information of this kind for the new Axiom
user.
>
> Thank you for all your help
>
You are welcome! :)
Regards,
Bill Page.
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- [Axiom-developer] RE: [Axiom-math] if-then-else: what is wrong?,
Bill Page <=