function argument overwriitten?



As Stavros Macrakis said, matrices and lists are modified globally,  so if you 
want your function not to modify your inputs, you have to create a separate 
copy of them.
In your example, because a is a list, you have to use copylist. Now t will be 
completely independent and a won't be modified. This is the only solution (I 
think).

(%i2) f(v):=block([t],t:copylist(v),t[1]:99,t);
(%o2) f(v):=block([t],t:copylist(v),t[1]:99,t)
(%i3) a:[1,2,3];
(%o3) [1,2,3]
(%i4) m:f(a);
(%o4) [99,2,3]
(%i5) a;
(%o5) [1,2,3]

If you rinput is a matrix, use copymatrix instead.
Hope this helps.

Stefano


In data venerd? 22 maggio 2009 15:28:51, Rob Burns ha scritto:
: > This seems like an error to me....
>
> What's going on here?
> Create a function..
> (%i6) f(v):=block([t],t:v,t[1]:99,t);
> (%o6) f(v):=block([t],t:v,t[1]:99,t)
> (%i7) a:[1,2,3];
> (%o7) [1,2,3]
> and execute it
> (%i8) m:f(a);
> (%o8) [99,2,3]
> it has worked ok...
> (%i9) m;
> (%o9) [99,2,3]
> BUT the source operand has been overwritten !!
> (%i10) a;
> (%o10) [99,2,3]
>
> Also the same thing happened when I mistakenly put a for v in the
> definituion of f()
>
> Create a function..
> (%i1) f(v):=block([t],t:a,t[1]:99,t);
> (%o1) f(v):=block([t],t:a,t[1]:99,t)
> (%i2) a:[1,2,3];
> (%o2) [1,2,3]
> and execute it
> (%i3) m:f(a);
> (%o3) [99,2,3]
> it has worked ok...
> (%i4) m;
> (%o4) [99,2,3]
> BUT the source operand has been overwritten !!
> (%i5) a;
> (%o5) [99,2,3]
>
> Am I missing something?
> Regards, Rob.
>
>
>
>
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima