how to pass the name of a variable as argument?



Hm, doesn't work in all cases (I'm using 5.15.0):

f(x) := x :: adjoin(1,ev(x));
x : {8,9};
f('x);
Function adjoin expects a set, instead found x
#0: f(x=x)
 -- an error.  To debug this try debugmode(true);

Apparently the problem is a confusion about the role of "x".

a : {3,4};
f('a);
a;
{1, 3, 4}

On Thu, May 01, 2008 at 08:42:29PM -0400, Stavros Macrakis wrote:
> On Thu, May 1, 2008 at 8:14 PM, Oliver Kullmann
> <O.Kullmann at swansea.ac.uk> wrote:
> >  Since global variables should be avoided, a good
> >  solution would be to pass the name of a variable
> >  to f, so that then we could use
> 
> I am not sure that global variables are any worse than this method, but....
> 
> To name a variable, use 'var.  To assign to a variable through its
> name, use var :: newval. To access the value of the value of a
> variable, use ev(var).  So your example becomes
> 
>     f(x) := x :: adjoin(1,ev(x));
> 
> and is called as
> 
>     f('var)
> 
> I don't know what your overall problem is, but I think I would just
> use global variables with a naming convention, e.g. global_x,
> global_y, or a hasharray global['x], global['y].
> 
>

Problems arise with testing:
I have a "higher-order unit test framework", where in the above case f is
passed as a parameter to the generic test-function: test-functions test
general concepts (in the above case that of adjoining 1 to a set), and not
specific implementations. Now when using such a global naming scheme, then I get
this additional coupling, where I need to associate this special naming scheme
to f, so that also this functionality can be tested (i.e., whether the
partial results are correctly accumulated).
And this association of f to its accompanying result-variable seems
harder to achieve (in general!) than to use the more general scheme for implementing f
where the result-variable is a parameter.

So well, to avoid the above problems, the parameter for f apparently
needs to be given a longer ugly name, so that name clashes are less
likely.

Thanks!

Oliver