axiom-mail
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Axiom-mail] Declare variable type within function


From: Bill Page
Subject: Re: [Axiom-mail] Declare variable type within function
Date: Mon, 17 Mar 2008 12:06:54 -0400

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




reply via email to

[Prev in Thread] Current Thread [Next in Thread]