Local variables not so local within block



Another example. Here the values assigned inside the block are assigned to
the external (global) undeclared array

(%i1) f(x):=block([a],a:zeromatrix(2,2),a[1]:[2,2]);
(%o1) f(x):=block([a],a:zeromatrix(2,2),a[1]:[2,2])

(%i2) a[1]:1;
(%o2) 1

(%i3) f(5);
(%o3) [2,2]

(%i4) a[1];
(%o4) [2,2]

So the global undeclared array has been filled by the assignement inside
the block...  but if a is killed no values are "exported" outside the block:

(%i11) kill(a);
(%o11) done

(%i12) f(5);
(%o12) [2,2]

(%i13) a[1];
(%o13) a[1]






2012/7/22 Stefano Ferri <ferriste at gmail.com>

> I'm having some difficulties with a conflict between a local variable used
> inside a block construct and a global variable of different type. In
> particular, the conflict is between a matrix and an undeclared array.
> Here is the simplest example I've found to show the problem. Indeed, the
> function f is doing nothing useful, it is just an example.
>
> (%o1) build_info();
>
> Maxima version: 5.24.0
> Maxima build date: 10:27 5/17/2011
> Host type: i686-pc-linux-gnu
> Lisp implementation type: GNU Common Lisp (GCL)
> Lisp implementation version: GCL 2.6.7
>
> (%i2) f(x):=block([a],a:zeromatrix(2,1),a[1,1]:x);
> (%o2) f(x):=block([a],a:zeromatrix(2,1),a[1,1]:x)
>
> (%i3) f(5);
> (%o3) 5
>
> But if we define a global undeclared array with the same name of the local
> matrix inside block:
>
> (%i4) a[1]:1;
> (%o4) 1
>
> (%i5) f(5);
>
> assignment: array a has dimension 1, but it was called by a[1,1]
> #0: f(x=5)
>  -- an error. To debug this try: debugmode(true);
>
>
> The probem is the conflict between the last assignement inside f(x), that
> is a[1,1]:x, and a definition of a global undeclared array with the same
> name of the internal variable a. If a[1,1]:x is removed from f, or if the
> global array is killed, the problem disappears. Global matrices and lists
> are safe, I've noticed this problem only with undeclared array.
>
> So, why a local variable can see what is outside the block?
>
>
> Stefano
>