MathGroup Archive 2007

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

Search the Archive

Re: Get list of function variables?

Jason Quinn wrote:

 > This seems simple but how do I get the arguments to an arbitrary
 > function? Ideally something like getVariables[f] that just accepts the
 > function name as its argument.
 > If I have f[x_,y_]:=a*x^2+b*y^2,  getVariables[f] should return {x,y}
 > If I have g[n_,m_]:=a*n^2+b*m^2, getVariables[g] should return {n,m}
 > If I have h[w_,x_,y_,z_]:=A*w*x*y*z+B, getVariables[h] should return
 > {w,x,y,z}
 > Thanks for any suggestions,


To make a long story short, in Mathematica, there is no such things as 
"variables" (at least not in the sense as the term is used for function 
definition in more classic languages). Mathematica programming language 
is not like C/C++/C#/Pascal/BASIC/JAVA/etc. It is not object oriented. 
It not strongly typed. It is relatively inefficient in 
procedural/imperative paradigm. It is much more like LISP/Scheme, i.e. 
functional paradigm. And overall, it is a pattern rewriter.

So, when defining a function, you are not defining any (typed) variables 
but you give patterns and name them if needed, with some associated 

All the following function definitions are valid. What would a function 
like getVariables could possibly returned when called for f or g?

In[1]:= f[___] := False
f[2] = True;
f[_List] = "Lists are not allowed!";
g[x_, y_] := a*x^2 + b*y^2
g[x_Integer, y_Integer] := Mod[x, y]
g[x_?MatrixQ, y_?VectorQ] := x.y

In[7]:= f[a, s, d, g]

Out[7]= False

In[11]:= f[{a, s, d, g}]

Out[11]= "Lists are not allowed!"

In[12]:= f[2]

Out[12]= True

In[13]:= f[]

Out[13]= False

In[16]:= g[1, 2.]

Out[16]= a + 4. b

In[17]:= g[1, 2]

Out[17]= 1

In[18]:= g[a, b]

Out[18]= a^3 + b^3

I hope I have shed more light than obscurity!


  • Prev by Date: Re: Does ColorFunction-> have different meanings to Plot3D and
  • Next by Date: Re: memory release problem in mathematica6.0
  • Previous by thread: Re: Get list of function variables?
  • Next by thread: Re: Get list of function variables?