MathGroup Archive 2002

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

Search the Archive

RE: Re: Function as an argument of the function


That code doesn't even execute:

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

Function::flpar: Parameter specification vars_ in Function[vars_,expr_]
\
should be a symbol or a list of symbols.

If you decide to check DownValues, don't forget to check SubValues as
well.

However, built-in functions will defeat that method and perhaps ANY
method.

Bobby Treat

-----Original Message-----
From: Orestis Vantzos [mailto:atelesforos at hotmail.com] 
To: mathgroup at smc.vnet.net
Subject: [mg34697] [mg34677] Re: Function as an argument of the function

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: Increased Precision in Plot?
  • Next by Date: RE: Re: Function as an argument of the function
  • Previous by thread: RE: Re: Function as an argument of the function
  • Next by thread: RE: Re: Function as an argument of the function