local() not working?



On Fri, 25 Jun 2010 19:45:51 +0800, Barton Willis <willisb at unk.edu> wrote:

> -----maxima-bounces at math.utexas.edu wrote: -----
>
>> Maybe I am missing something, but shouldn't the following code leave 'var'
>> undefined? It works using square brackets instead of local:
>>
>> var;
>> (%o1) var
>>
>> func(arg) := block(
>>      local(var),
>>      var: 7,
>>      return(42)
>> )$
>>
>> func(100);
>> (%o3) 42
>>
>> var;
>> (%o4) 7
>
> To declare a local variable, use something like this:
>
>  (%i1) func(arg) := block([var],
>        var: 7,
>        return(42))$
>
>  (%i2) func(100);
>  (%o2) 42
>
> Okay--var doesn't have the value 7:
>
>  (%i3) var;
>  (%o3) var
>
> To learn about 'local,' enter "? local" to read the user documentation. Here is an
> example of using 'local':
>
>  (%i4) larry(x) := block([var],
>     var[2] : 5,
>     42)$
>
>  (%i5) larry(100);
>  (%o5) 42
>
>  (%i6) var[2];
>  (%o6) 5
>
> Now use local
>
>  (%i7) george(x) := block([w],
>     local(w),
>     w[2] : 5,
>     42)$
>
>  (%i8) george(100);
>  (%o8) 42
>
>  (%i9) w[2];
>  (%o9) w[2]
>
> I believe that the Maxima translator does not work with local; after compiling
> the function 'george', we have a problem:
>
> (%i10) compile(george);
> Local does not work well in translated code.
> Try to use value cell and the Use_fast_arrays:true
>if this is for arrays.  For functions, local definitions are
> not advised so use lambda expression
>
>  (%i11) george(100);
>  (%o11) 42
>
> Wrong:
>
>  (%i12) w[2];
>  (%o12) 5
>
> --Barton
>

Thanks Barton. I had read the help, but I didn't realise that the value of a variable isn't a property :)
It is a little misleading really, as the example shows a function definition being locally overwritten without
any square brackets, and one tends to assume that functions are values too, esp in lisp.

James