[Albert Reiner <areiner@tph.tuwien.ac.at>] Re: [Maxima] splicing list of optional arguments in function call



[Richard Fateman <fateman@cs.berkeley.edu>, Tue, 14 Sep 2004 08:40:03 -0700]:
> I don't know what exactly you are trying to do, but
> maybe you can make your life simpler by not trying to
> call foo again or using buildq.  Use a different function, and one with a
> fixed number of arguments.

Thanks, that always works of course - I hadn't thought about this.

> You should think twice before using buildq.  And then
> think twice again.  Unless you require unconventional
> evaluation of arguments, it is probably a mistake to
> use it.

I have played around a bit, and I can see that buildq is problematic,
not only for this case where there is a very simple solution.

The issues I have found all have to do with variable capture: e.g.,
with my original foo() I get

(C1) foo(a,b,c,[z]) :=
  if inpart(a,0) = "="
  then
    buildq([a: inpart(a,1)-inpart(a,2), b, c, z],
           lambda([], foo(a,b,c,splice(z)))) ()
  else
    [ a, b, c, z ] $

(C2) foo(x=y,1,2,a);

(D2)                        [x - y, 1, 2, [x = y]]
(C3) foo(x=y,1,2,m);

(D3)                          [x - y, 1, 2, [m]]
(C4) foo(x=y,1,2,z);

(D4)                         [x - y, 1, 2, [[z]]]

The last of these can easily be fixed with a gensym,

(C10) splice_eval(var, expr) ::=
  block([gs: ?gensym()],
        buildq([var, gs, expr: subst(gs, var, expr)],
               buildq([gs:var], lambda([], block([var], expr)))())) $

(C11) foo(a,b,c,[z]) :=
  if inpart(a,0) = "="
  then
    splice_eval(z, foo(inpart(a,1)-inpart(a,2), b, c, splice(z)))
  else
    [ a, b, c, z ] $

(C12) foo(x=y,1,2,a);

(D12)                       [x - y, 1, 2, [x = y]]
(C13) foo(x=y,1,2,m);

(D13)                         [x - y, 1, 2, [m]]
(C14) foo(x=y,1,2,z);

(D14)                         [x - y, 1, 2, [z]]

but the capture of a in C2, C12 remains, and right now I do not see
how to avoid that in a simple way.  The two-function approach does not
suffer from this, of course.

Are there any other issues with buildq that I should be aware of?

Thanks again,

Albert.