Re: Declare variable type within function

"Bill Page" <[email protected]>
Newsgroups gmane.comp.mathematics.axiom.user
Message-ID <[email protected]>
Welcome to Axiom!

On 3/17/08, Angelos Mantzaflaris wrote:
>
> Here is a newbie question: I am writing some axiom scripts for
> which I don't want to declare I/O types.

In general the ability to write scripts without declaring types in
Axiom is rather limited. Axiom is strongly typed (everything has a
type) but the type inference in the Axiom command line interpreter is
rather weak and sometimes can produce very unexpected results. So I
would first comment: "continue on this path with caution"...

> But, I need to declare, for example, an empty
>  list of typeOf(input). I have tried the following:
>
>  foo(x)==
>         L:=[]
>         L:= append(L,x)
>         return L
>
>  Axiom will complain:
>  The type of the local variable L has changed in the computation.
>  We will attempt to interpret the code. I also try:
>
>  foo(x)==
>         L:=[]::typeOf(x)
>         L:= append(L,x)
>         return L
>
>  Axiom will complain again:
>  Cannot compile conversion for types involving local variables.
>  In particular, could not compile the expression involving
> :: typeOf AXIOM will attempt to step through and interpret the
> code.

Axiom is not complaining, it is only giving you a warning that it
cannot compile what you wrote (for the reason it gave) but it still
might be able to interpret it - and does. Is there a reason why you do
not want this to happen?

>
>  But I can play tricks like this:
>
>  foo(x)==
>         L:=[x.1-x.1]
>         L:= append(L,x)
>         return L
>
>  And Axiom will compile the function without errors:
>  Compiling function foo with type List PositiveInteger List Integer
>
>
>  So my question is, how can I do this in a straightforward way
> (without playing tricks)?
>

Although you might still consider it a "trick" I would suggest the
following code that I think is more transparent:

  foo(x)==
         L:=select(a+->false,x)
         L:= append(L,x)

Personally, in the context where you want to use this ("scripts
without declaring types"), I would not regard this as a trick. It
seems likely however that in most cases it should be possible to write
the code you want without having to first create an empty list by
using higher-order functions like 'select' and 'map'.

Please feel free to post examples of any problems you encounter.

Regards,
Bill Page
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.