       Re: Get list of function variables?

• To: mathgroup at smc.vnet.net
• Subject: [mg83310] Re: Get list of function variables?
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Sat, 17 Nov 2007 05:22:34 -0500 (EST)
• Organization: The Open University, Milton Keynes, UK
• References: <fhjs5v\$4t3\$1@smc.vnet.net>

```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,

Jason,

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
conditions.

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

In:= f[___] := False
f = 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:= f[a, s, d, g]

Out= False

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

Out= "Lists are not allowed!"

In:= f

Out= True

In:= f[]

Out= False

In:= g[1, 2.]

Out= a + 4. b

In:= g[1, 2]

Out= 1

In:= g[a, b]

Out= a^3 + b^3

I hope I have shed more light than obscurity!

Regards,
--
Jean-Marc

```

• 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?