Bill Allombert on Sat, 11 Oct 2014 16:42:08 +0200


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

Re: Question about apply


On Fri, Oct 10, 2014 at 06:34:38PM +0200, Josef Eschgfaeller wrote:
> I cannot understand the following behavior:
> --------------------------------------------------
> v=[1,2,3,4]
> 
> a=[]; b=[];
> apply (x->a=concat(a,[x]),v);
> print(a)
> 
> \\ [1, 2, 3, 4]
> 
> \\ Same within a function:
> f (v) = {my (a,b);
> a=[]; b=[];
> apply (x->a=concat(a,[x]),v);
> print(a)}
> 
> f(v)
> \\ []
> --------------------------------------------------

In the second example, the variable 'a' is statically scoped in in the function
'f'. Thus changes to 'a' inside another function (x->a=concat(a,[x]) in the example)
are not visible by f.

If you like your example to work, you need to use local() instead of my().

Cheers,
Bill.