scope, and returning values by changing input args



It is not a good idea to try to change an input variable
in macsyma programs  (or, for that matter, in any programming
language which uses essentially call-by-value.  You are using
call-by-value to pass the name of a global variable to be
changed... and you have encountered the problem that the
name you used has a binding in a different environment than
the one you anticipated.  Macsyma asks "what is 'ind'?" and
looks at all the binding that have been made, starting with
the most recent in time, and the first 'ind' it finds is the
one it uses.  This is dynamic scope.  Same as what maclisp
had when macsyma was written.  Lexical scope would give you
the binding of ind in the scope of the definition of the
calling program.

It is not worth puzzling over this if the fix I gave you
fixes it, I guess.

RJF

Dan Stanger wrote:

> I have a recursive function, which modifies its input variable, ind.
> 
> charsets_subrank ( f,g,ord,ind ):=block( [ cf,cg],
>       cf : charsets_class ( f, ord )  ,
>       cg : charsets_class ( g, ord )  ,
>       if cf = 0 then (
>             if cg = 0 then (ind :: 1) , TRUE)
>          else if cf < cg then ( TRUE)
>          else if cf = cg then (
>             cf : charsets_degreel ( f, ord )  ,
>             cg : charsets_degreel ( g, ord )  ,
>             if cf < cg then ( TRUE)
>                else if cf = cg then (
>                   charsets_subrank ( charsets_initial ( f, ord ) ,
>                        charsets_initial ( g, ord ) , ord, ind ) )
>                else ( FALSE))
>          else ( FALSE)
> )$
> 
> Following your sugestion, I renamed ind to ?ind.
> However, could you explain what dynamic scope means in this context?
> Dan Stanger
> 
> Richard Fateman wrote:
> 
> I'm not sure it is a bug since it seems to do what I expected
> it to do (it is a consequence of Macsyma generally using
> "dynamic scope").
> 
> Maybe the question is, what do you really want to do?
> 
> One change would be to define f(?funny_name):=?funny_name::1;
> to avoid the name coincidence.
> Another is to use a macro definition  f(x)::= ... with a
> suitable right hand side.  I'm not sure what exactly that should
> be though. The describe(macros) command in xmaxima comes up
> blank.
> 
> 
> Dan Stanger wrote:
> 
> 
>>The following is a problem with both maxima 5.5 and the commercial
>>macsyma.  What would it
>>take to fix it?
>>Dan Stanger
>>(C1) f(x):=x::1;
>>(D1)       f(x) := x :: 1
>>(C2) f(x);
>>(D2)       1
>>(C3) x;
>>(D3)       x
>>x is not assigned to.
>>(C4) y;
>>(D4)       y
>>(C5) f(y);
>>(D5)       1
>>(C6) y;
>>(D6)       1
>>However y is.
>>
>>
> 
> _______________________________________________
> Maxima mailing list
> Maxima@www.math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
>