using define inside a function



On Dec. 6, Barton Willis wrote:
--------------
>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.
--------------------

see test6 below:


and On Dec. 6, Robert Dodier wrote:
---------------------
>'ll recommend buildq since it is easier to see
>that you'll get. In this case you want something like
>define(h(x), buildq([foo:x, bar:y, baz:z], simp1(foo, bar, baz)));

see test5 below
-------------------

(%i1) load(simpson);

(%o1) "c:/work2/simpson.mac"

(%i2) test5(expr,x,y) :=
  block([x0,h],local(h),
    define(h(x0),
           buildq([%expr:subst(x0,u,expr)],
                 simp1(%expr,v,0,1,4))),
    print("  h(0.2) = ",h(0.2)),
    print("  h(0.5) = ",h(0.5)),
    disp("bye"))$

(%i3) simp1(0.2*v^2,v,0,1,4);

(%o3) 0.066666666666667

(%i4) test5(u*v^2,u,v);

  h(0.2) =  0.066666666666667
  h(0.5) =  0.16666666666667
"bye"

(%o4) done

(%i5) test6(expr,x,y) :=
  block([%expr,h:buildq([expr,x,y],
          lambda([xx],
             %expr:subst(xx,x,expr),
             apply('simp1,[%expr,y,0,1,4])))],
    print("  h(0.2) = ",h(0.2)),
    print("  h(0.5) = ",h(0.5)),
    disp("bye"))$

(%i6) test6(u*v^2,u,v);

  h(0.2) =  0.066666666666667
  h(0.5) =  0.16666666666667
"bye"

(%o6) done
------------------------------

>What is the definition of simp1, by the way?

very pedestrian and fortran reminiscent:
(sorry for the hard to read code)
---------------------------------------
simp1(expr,var,a,b,n):=
    block ([hh,_j%],local(_ff%),
     if not evenp(n) then return("n must be an even integer"),
     define(_ff%(var),expr),     
     hh : abs (b-a)/n,
     (hh/3)*( _ff%(a) + _ff%(b) + 
             4*sum( _ff%(a + (2*_j%-1)*hh), _j%, 1, n/2 ) +
             2*sum( _ff%(a + 2*_j%*hh), _j%, 1, (n/2) - 1 ) ),
     float(%%) )$
 -------------------------------

Thanks for the suggestions.

Ted