MathGroup Archive 2009

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

Search the Archive

Re: Determine if a parameter is a function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg101834] Re: Determine if a parameter is a function
  • From: ADL <alberto.dilullo at tiscali.it>
  • Date: Sat, 18 Jul 2009 08:00:47 -0400 (EDT)
  • References: <h3kddu$g35$1@smc.vnet.net>

Another possibility is to define a "question" function like this:

ClearAll[FunctionQ];
FunctionQ[f_] :=
    (Head[f] === Function) ||
    (AtomQ[f] && Head[f] === Symbol && !NumericQ[f] && (
        DownValues[f] =!= {} || Attributes[f] =!= {}
    ))

Then you can use the above function as an argument check like below:

ClearAll[myfunc];
myfunc::nfun = "argument `1` should be a function.";
myfunc[f_?FunctionQ, x_] = f[x];
myfunc[__] := Message[myfunc::nfun, f];

and the examples provided in the discussion work.

An borderline case might be:
myfunc[C, x]
C[x]
because C is a standard name for constants and has a particular
definition.

Note that the check for NumericQ is necessary to avoid slipping in
constants like I or E.

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



  • Prev by Date: Re: Naming Operators in Pure Function form
  • Next by Date: Player Pro Limitations
  • Previous by thread: Re: Determine if a parameter is a function
  • Next by thread: Re: Determine if a parameter is a function