Re: Determine if a parameter is a function

• To: mathgroup at smc.vnet.net
• Subject: [mg101754] Re: Determine if a parameter is a function
• From: Szabolcs <szhorvat at gmail.com>
• Date: Thu, 16 Jul 2009 08:18:22 -0400 (EDT)
• References: <h3kddu\$g35\$1@smc.vnet.net>

```On Jul 15, 2:09 pm, Peter Breitfeld <phbrf 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
>

I don't believe that it is possible to do this in a reliable way, so
my suggestion is that you don't test this at all.  Trying to test it
might cause more harm than good.

A "function" defined like f[x_] := x^2 will have DownValues (check
DownValues[f]).  But now consider something like g[n_][x_] := x^n.  I,
as a user, would expect that I can pass e.g. g[2] to your function,
but g[2] is neither a Function, nor a Symbol ... furthermore, g
doesn't even have DownValues (only SubValues).  Built-in functions
don't even have DownValues (or even worse: will only have them after
they have been used for the first time in a session).

Also, the fact that a symbol has DownValues is no guarantee that it
behaves like a "proper" function..

Szabolcs

P.S.  If myfunc[] takes functions in the mathematical sense (rather
than in a programming sense), sometimes it is advantageous to use a
syntax similar to myfunc[1+x^2+x^3, x] rather than myfunc[ 1+#^2+#^3
& ].  However, whether this is an advantage or disadvantage depends on
what myfunc[] actually is/does.

```

• Prev by Date: Re: Assigning Values to Many Symbols at Once
• Next by Date: Re: Re: Refine, assumptions, domains
• Previous by thread: Re: Determine if a parameter is a function
• Next by thread: Re: Determine if a parameter is a function