scope of function-parameters




On Thu, 26 Aug 2010, UlrichP at habmalnefrage.de wrote:

< Hello,
< 
< I would appreciate very much if anybody could give me (a hint to) information about the difference of the "square brackets" ([ ]) in a block and the "local"-statement. I don't find the manual very clear in this point.
< 
< The problem:
< 
< I wanted to use a function as a parameter -- every calculus student knows want is meant by
< 
< /*   */
< dqf(f, a, h):= (f(a+h) - f(a))/h;
< /*   */
< 
< This definition, which works fine t.ex. with Maple (of course with a different syntax), doesn't work with Maxima: if f is already (globally) defined at the moment of the definition of dqf (that means if f is bound) then this global f is used!
< 
< I made a simpler example (but with some local variable to keep things general enough) -- and after many tries I succeded: but, to be serious, I don't know why:
< 
< /*   */
< kill(all);
< f(x):= sin(x);
< ff(x):= cos(x); /* try even WITHOUT this statement to see that we need ff in [ ] too! */
< testF(f, x):= block([ff, a: 3],
<   local(ff),
<   ff: f,
<   a*ff(x));
< g(x):= x^2;
< testF(g, 2);
< f(2);
< ff(2);
< /*   */
< 
< Can this be done simpler? What is the idea of "dynamical binding" (with respect to function-parameters) in Maxima?

I think that your example is fine, but you don't need quite so many
layers:
 
(%i2) f(x):= sin(x) $
testF(f,x,h) := (local(f),(f(x+h)-f(x))/h) $
testF(tan,y,s);

(%o4) (tan(y+s)-tan(y))/s


'local' does what it says: it removes any bindings or other properties
of a symbol within a block, lambda expression or rhs of a function
definition. Note that since I don't have any local variables in testF,
I don't need a block. 

I don't really have a good explanation why the function property is
treated differently from other properties of a symbol, though. What I
mean is that ':=' correctly makes x and h locals (e.g. it ignores their
values if they have any) but you need to make f local yourself.

Leo

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.