using define inside a function



 -----maxima-bounces at math.utexas.edu wrote: -----

>In what follows, I use two different methods
>(in test1 and test2 respectively)
>to define the local function h(x) (or h(var)).

My favorite way to locally define a function is to use a lambda form.
Two reasons: (1) The local statement translates incorrectly. (2) There
is (at least for me) less uncertainty about what is done when.

Example:

(%i1) bob(e,x) := block([f],
  local(f),
  define(f(x),e),
  f(1))$

(%i2) sally(e,x) := block([f : buildq([x,e], lambda([x], e))],  f(1))$

Both OK:

 (%i3) bob(z^2 + 5,z);
 (%o3) 6

 (%i4) sally(z^2+5,z);
 (%o4) 6

 (%i5) translate(bob)$
 warning: f is a bound variable in f(1), but it is used as a function.
 note: instead I'll translate it as: apply(f,[1])

Local translates incorrectly:

 (%i6) bob(x^2+5,x);
 Maxima encountered a Lisp error:
  Error in APPLY [or a callee]: The function MDEFINE is undefined.

 (%i7) translate(sally);
 warning: f is a bound variable in f(1), but it is used as a function.
 note: instead I'll translate it as: apply(f,[1])

 (%o7) [sally]

Warning, but OK:

 (%i8) sally(x^2+5,x);
 (%o8) 6

--bw