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: [mg101809] Re: [mg101722] Determine if a parameter is a function
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Sat, 18 Jul 2009 04:48:38 -0400 (EDT)
  • References: <200907151110.HAA16514@smc.vnet.net>

Hi Peter,

In general, I add my voice to what Szabolcs said - he
formulated the difficulties associated with such a check very nicely.
IMO, trying to implement full typing in an untyped language is generally
not a rewarding activity.

What I do sometimes for my own functions is to add a check of the
form

fn[x:(_Symbol|_Function)],

which is not a complete test (so does not guarantee the correctness
of execution with a passed argument, especially for Symbol), and also,
as  Szabolcs pointed out, excludes the SubValues case. But this is often good
enough to catch most errors. If you start using constructs for which
this will not be good enough, you will for sure be able to find acceptable
for you ways to work around this problem.


Regards,
Leonid



On Wed, Jul 15, 2009 at 4:10 AM, 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
> have Head Symbol, so asking for the head is not sufficient.
>
> --
> _________________________________________________________________
> Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de
>
>


  • Prev by Date: Re: How to make variables local to a particular notebook?
  • Next by Date: Re: How to make variables local to a particular
  • Previous by thread: Determine if a parameter is a function
  • Next by thread: Re: Determine if a parameter is a function