# Checking if input is a function

I would like to write a program to find the leading coefficient of a given polynomial. (Sounds easy, right?) I'd like to be able to handle the format x^2-x+7 (with any formal variable x) as well as the format #^2-#+7&. I would also like to be able to reject/error on inputs which are not univariate polynomials.

I thought the function Variables would make this easy, but it turns out that

Variables[#^2-#+7&]


gives

{#1^2 - #1 + 7 &}

which isn't helpful. My current plan:

1. Check if the input is a function; if so, substitute f[\[FormalX]] for f.
2. Check if Variables[f] has length other than 1; if so, fail.
3. Define x as Variables[f][] and return Coefficient[f,x,Exponent[f,x]].

But I'm stuck on the first step. Is there a good way to do this? Is there a better approach to what I'm trying to do?

• Does checking the head work, i.e. yourFunction[x_Function]:= do something to x.? Apr 25, 2016 at 16:39
• Variables[#^2 - # + 7] will work, tho. Apr 25, 2016 at 16:40
• @J.M. That's interesting -- is there a way to convert an anonymous function to this form automatically? Apr 25, 2016 at 16:41
• Variables@First@Function[#^2 - # + 7] Apr 25, 2016 at 16:52
• BTW, you might also be interested in PolynomialQ[]. Apr 25, 2016 at 16:59

lead[f_Function ] := lead[f@\[FormalX]]