Re: Determine if a parameter is a function
- To: mathgroup at smc.vnet.net
- Subject: [mg101838] Re: Determine if a parameter is a function
- From: ADL <alberto.dilullo at tiscali.it>
- Date: Sun, 19 Jul 2009 07:13:25 -0400 (EDT)
- References: <h3kddu$g35$1@smc.vnet.net>
Actually, after some work, I found a solution which appears more
reliable (but who knows...):
ClearAll[FunctionQ];
FunctionQ[f_] := (functionQtest2[f] || functionQtest3[f]) &&
functionQtest1[f];
ClearAll[functionQtest1,functionQtest2,functionQtest3];
functionQtest1[f_Symbol] := (Length[StringPosition[ToString
[f::usage,OutputForm],"["]]>0);
functionQtest1[___] := True;
functionQtest2[f_] := (Head[f]===Function);
functionQtest3[f_] := (AtomQ[f] && (Head[f]===Symbol) && (!NumericQ
[f]) && (
(DownValues[f]=!={})||(Attributes[f]=!={})
));
The functionQtest1 test comes from a little heuristics about usage
messages.
Then, the following comes out:
In:= FunctionQ[Red]
Out= False
In:= FunctionQ[\[ScriptCapitalA]]
Out= False
In:= FunctionQ[Log]
Out= True
In:= FunctionQ[Algebraics]
Out= False
In:= FunctionQ[Log[Sin[#]] &]
Out= True
In:= systemFunctions = Select[ToExpression /@ Names["System`*"],
FunctionQ];
In:= systemFunctions // Length
Out= 1661
ADL
On Jul 15, 1:09 pm, Peter Breitfeld <ph... at t-online.de> wrote:
> Suppose I have a function eg
>
> myfunc[f_,x_]:= <some definitions>
>
> f should be a pure function like (#^2&) or Function[{x},x^2] or a named
> function either self defined, like
>
> f[x_]:=x^2 or g[x_]=x^2
>
> or built-in like Sin, Log, ...
>
> How can I test if f is any of these, to be able to yield a message on
> wrong input?
>
> I found that the pure-functions have Head Function, but all the others
> have Head Symbol, so asking for the head is not sufficient.
>
> --
> _________________________________________________________________
> Peter Breitfeld, Bad Saulgau, Germany --http://www.pBreitfeld.de