MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Q: extracting list of arguments of function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg16610] Re: [mg16468] Q: extracting list of arguments of function
  • From: "Wolf, Hartmut" <hwolf at debis.com>
  • Date: Fri, 19 Mar 1999 12:53:44 -0500
  • Organization: debis Systemhaus
  • References: <199903130722.CAA24544@smc.vnet.net.>
  • Sender: owner-wri-mathgroup at wolfram.com

Hello Jerzy

Jerzy Niesytto schrieb:
> 
> Let's assume that we want to pass function goo as argument
> to another function foo and extract list of arguments
> of the function goo:
> foo[fun_] := Module[{argList}, argList = Table[fun[[i]],{i,1,2}] ]
> foo[goo[x,y]];
> 
> This will result in list:
> {x,y}
> 
> But if goo was defined before eg:
> goo[s_,t_] = s^2 + t^3;
> 
> then we get:
> {x^2,y^3} .....
> 
> Can anybody point me in a right direction here?

Jerzy you tried to extract the arguments of a function as a List, I
propose to extract them as the object it *is*, namely a Sequence (i.e.
an argument sequence). To do so, just Delete the Head of g[x,y] :

In[1]:= Delete[g[x,y],{0}]
Out[1]= Sequence[x,y]

But that doesn't work when g ist defined 

In[2]:= g[x_,y_]:=x^2-y^2

In[3]:= Delete[g[r,s],{0}]
Out[3]= Sequence[r^2, -s^2]

This is so because g[r,s] already gets evaluated within Delete. To
obviate that we enclose the function g with Hold, then we have to first
delete the Head g within Hold, and then the remaining Head Hold:

In[4]:= Delete[Hold[g[x,y]],{{1,0},{0}}]
Out[4]= Sequence[x,y]

If you insist in having that as a List, just guide this object (argument
sequence) to List

In[5]:= List[%]
Out[5]= {x,y}

However, if the arguments to the function g already got a value, then...

In[25]:= x=2; y=4; z=x+y;

In[26]:= Delete[Hold[g[x,y,z]],{{1,0},{0}}]
Out[26]= Sequence[2,4,6]

...you'll get the calling sequence with the actual calling parameters.
This may be exactly what you want. Otherwise keep the outer Hold

In[27]:= Delete[Hold[g[x,y,z]],{1,0}]
Out[27]= Hold[x,y,z]

But whenever you touch at the arguments you'll get their values back

In[28]:= List@@%
Out[28]= {2,4,6}

You still have a different option to first wrap the arguments with Hold:

In[49]:= Map[Hold,Hold[g[x,y,z]], {2}]
Out[49]= Hold[g[Hold[x],Hold[y],Hold[z]]]

In[50]:= Delete[%,{{1,0},{0}}]
Out[50]= Sequence[Hold[x],Hold[y],Hold[z]]

But effectively Out[27] and Out[49]+Out[50] just tells you that g was
called with 3 Parameters, and nothing more! And if you called g by
values, you get

In[54]:= Delete[Map[Hold,Hold[g[x,3,z+1]], {2}],{{1,0},{0}}]
Out[54]= Sequence[Hold[x],Hold[3],Hold[z+1]]

Which is fine if your are writing a debugger. I don't know your real
problem, but my recommendation is In[26]. 

Of course all this also works when calling pure functions

In[30]:= Delete[Hold[(#1 E^#2+Sin[#3]&)[x,y,z]],{{1,0},{0}}]
Out[30]= Sequence[2,4,6]

---Hartmut
__________________________________________________
Hartmut Wolf, debis Systemhaus, Darmstadt, Germany



  • Prev by Date: Re: classsical Programing vs a math package
  • Next by Date: Re: Problem: Smallest sphere including all 3D points
  • Previous by thread: Re: Q: extracting list of arguments of function
  • Next by thread: Re: Q: extracting list of arguments of function