[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Axiom-math] (no subject)
From: |
Martin Rubey |
Subject: |
Re: [Axiom-math] (no subject) |
Date: |
Tue, 21 Jun 2005 09:46:18 +0200 |
I copy this to axiom-math, because others might be interested.
C. Frangos writes:
>
> Dear Martin,
>
> I have modified your function test() in two ways (see below). Both
> modifications result in errors.
I give the reasons below.
> I have also attached an attempt at a simple package which does not compile.
dito.
> I am using the basic syntax given in the axiom book for command line
> programming, that is, semicolons at end of lines, brackets for function
> definitions, brackets for blocks in if-then-else, for-repeat statements,
> brackets for function calls, etc. I find this easier to remember and
> consistent. I dont favour indentation.
> It seems that this syntax cannot be used in .input and .spad files. As you
> mentioned axiom seems to be very indentation sensitive (first time I come
> across this in a compuetr language).
I don't like it either. Phyton does the same thing (probably got it from
Axiom). However, the rules are very simple:
One operation per line. No semicolon at the end.
A couple of operations indented by the same amount constitute a "pile". In C
and Aldor, they would be enclosed by { }.
A pile is needed after
* ==, i.e. a function definition
* for ... repeat
* if ... then
* else
That's all, I think.
> If this is right then these undesirable features alone makes it difficult to
> use axiom. The seemingly different syntax for command line programming
> versus
> .input and .spad files is not explained anywhere in the axiom book as far as
> I could see ???
Well, both forms are allowed in .spad and .input files. (op1; op2; ...; opn) is
simply a construct to fit n operations in a place where only one is allowed.
> (In axiom is there a read command ala C/C++ to allow the user to type in
> something, etc ??)
I'm afraid no. The approach is different. I'll help when we come to it.
> ------ File test.input
>
> g: INT := 5
>
> -- Your original code.
> --test():INT ==
> -- output(g)
> -- g := 6
> -- l: INT := 10
> -- output(l)
> -- l
>
>
> -- Code below compiles but identation causes error when you
> -- attempt to run test(); !!!!
correct.
> test():INT ==
>
> output(g)
>
> g := 6
>
> l: INT := 10
>
> output(l)
>
> l
You can see what )re test.input did to your function definition by typing
test
(without parenthesis) at the prompt:
(16) -> test
(16)
test () ==
output(g)
g := 6
l : INT :=
10
output(l)
l
The following would work.
test():INT ==
output(g)
g := 6
l: INT := 10
output(l)
l
>
> -- Code below does not compile.
> --test():INT == (
> -- output(g);
> -- g := 6;
> -- l: INT := 10;
> -- output(l);
> -- return(l) )
The basic rule in axiom is: one operation per line.
The (operation-1; operation-2; ...; operation-n) construct constitutes one
single operation (the
result is the result of operation-n), therefore it has to go on one line. The
following would work:
test():INT == (_
output(g);_
g := 6;_
l: INT := 10;_
output(l);_
return(l) )
-------------------------------------------------------------------------------
Only two simple indentation mistakes in the following:
> -------File zmean.spad
>
> F ==> Float
>
> )abbrev package XZMEAN xzmean
> xzmean(): Exports == Implementation where
> Exports == with
> mean: List F -> F
> stddev: List F -> F
> Implementation == add
>
> mean l ==
> n := #l
> if n = 0 then error "The mean of an empty list is not defined"
> reduce(_+, l)/n
>
> -- The following code does not compile.
> stddev l ==
> n := #l
> if n = 0 then
> ( error("The standard deviation of an empty list is not defined") )
^^^
should be
> ( error("The standard deviation of an empty list is not defined") )
> m := mean l
> s := 0.0
> for i1 in 1..n repeat
^^^
should be
> for i1 in 1..n repeat
> ( s := s + (l(i1) - m)**2 )
> s := sqrt(s/n)
> -- The following code also does not compile.
> -- stddev(l) == (
> -- n := #l;
> -- if n = 0 then
> -- ( error("The standard deviation of an empty list is not defined") )
> -- m := mean(l);
> -- s := 0.0;
> -- for i1 in 1..n repeat
> -- (s := s + (l(i1) - m)**2)
> -- s := sqrt(s/n);
> -- return(s) )
same reason as in the test.input example: (op1; op2;...;opn) is a single
operation, therefore must go on a single line. Use _ ! Furthermore, you forgot
two semicolons:
stddev(l) == (_
n := #l; _
if n = 0 then _
( error("The standard deviation of an empty list is not defined") ); _
m := mean(l);_
s := 0.0;_
for i1 in 1..n repeat_
(s := s + (l(i1) - m)**2); _
s := sqrt(s/n);_
return(s) )