On 10/22/10 at 1:35 AM, sam.takoy at yahoo.com (Sam Takoy) wrote:
>How does one tell whether a variable is a function? More
>specifically, how does one tell whether a variable is a function of
>two arguments? Finally, how does one tell whether a variable is a
>function of two arguments that returns an array?
A function will have down values but a variable will not. For example:
In[1]:= f[x_] := 2 x
g[x_] = 4 x;
DownValues /@ {f, g}
Out[3]= {{HoldPattern[f(x_)]:>2 x},{HoldPattern[g(x_)]:>4 x}}
In[4]:= y = 9;
DownValues[y]
Out[5]= {}
Further, you can use the down values of a function to answer
your other questions. For example:
In[6]:= k[x_, y_] := x y
Length[Cases[DownValues[k], _k, \[Infinity]][[1]]]
Out[7]= 2
In[8]:= m[x_] := {x}
Head@DownValues[m][[1, 2]]
Out[9]= List
But there is a difficulty with your other questions. For
example, I can do:
In[10]:= m[x_, y] := x y
In[11]:= DownValues[m]
Out[11]= {HoldPattern[m(x_)]:>{x},HoldPattern[m(x_,9)]:>x y}
Now, m can take either 1 or 2 arguments and returns either a
single value or an array depending on arguments. What I've done
above with DownValues to get the number of arguments and return
values only works for simply defined functions.