On Fri, 3 Sep 2010, Leo Butler wrote:
<
<
< 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
Mea culpa, I am wrong. You do need the level of indirection
in your example because local(f) removes the binding on f, if
you pass f as the first argument. However, I still see problems
if I pass ff.
Here is a definition that does work, using apply. I believe it
works because apply delays evaluation of its arguments until
testF is called:
ff(x):= cos(x) $
f(x):= sin(x) $
testF(f,x,h) := (apply(f,[x+h])-apply(f,[x]))/h $
testF(tan,y,s);
testF(f,t,s);
(%o5) (tan(y+s)-tan(y))/s
(%o6) (sin(t+s)-sin(t))/s
Leo
--
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.