MathGroup Archive 2002

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

Search the Archive

Re: Function as an argument of the function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg34677] Re: Function as an argument of the function
  • From: atelesforos at hotmail.com (Orestis Vantzos)
  • Date: Fri, 31 May 2002 04:27:47 -0400 (EDT)
  • References: <ad4jg1$jm2$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

maltomek at hotmail.com (Tomek) wrote in message news:<ad4jg1$jm2$1 at smc.vnet.net>...
> Hi.
> I have to write a short function in Mathematica. But I have one
> problem with  it. One of arguments of my function have to be a
> function of one variable.
> How can I check if the argument of the function is a function of one
> variable (I wish there's FunctionQ)?

Well, if you limit your choice down to pure functions, you can use the
following code to check whether the pure function accepts a single
argument:

OneArgFunctionQ[_] := False;
OneArgFunctionQ[Function[expr_]] := 
    FreeQ[expr, Slot[n_ /; n > 1]] && FreeQ[expr, _SlotSequence];
OneArgFunctionQ[Function[vars_, expr_]] := Length[vars] == 1;

Examples:
OneArgFunctionQ[#&] --> True
OneArgFunctionQ[#+#2&] --> False
OneArgFunctionQ[Function[{x},x^2]] --> True
OneArgFunctionQ[Function[{x,y},x+y]] --> False

If you want to check symbols like Sin, Cos, etc. things get ugly
really fast.
You could theoreticaly check the DownValues of the symbol, but I
wouldn't bother unless it was absolutely imperative.
Orestis


  • Prev by Date: RE: Extraction of positive values from list of both +/- values
  • Next by Date: RE: new user question about creating a new cell
  • Previous by thread: Re: Function as an argument of the function
  • Next by thread: Re: Function as an argument of the function