Re: extracting list of arguments of function
- To: mathgroup at smc.vnet.net
- Subject: [mg16485] Re: extracting list of arguments of function
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Tue, 16 Mar 1999 03:59:41 -0500
- References: <7cd7td$o8m@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Jerzy Niesytto wrote in message <7cd7td$o8m at smc.vnet.net>...
>Let's assume that we want to pass function goo as argument
>to another function foo and extract list of arguments
>of the function goo:
>foo[fun_] := Module[{argList}, argList = Table[fun[[i]],{i,1,2}] ]
>foo[goo[x,y]];
>
>This will result in list:
>{x,y}
>
>But if goo was defined before eg:
>goo[s_,t_] = s^2 + t^3;
>
>then we get:
>{x^2,y^3} .....
>
>Can anybody point me in a right direction here?
>Sorry for such an elementary question...
>
>JT
>
Jerzy:
Stop goo[x,y] from evaluating inside foo
SetAttributes[foo, HoldFirst]
Use Hold to stop it evaluatinfg after being passed, then get its arguments
using Level
foo[fun_] := Level[Hold[fun],{2}]
goo[x_,y_]:= x^2+y^2
foo[goo[x,y]]
{x,y}
If you want to prevent the arguments from evaluating:
SetAttributes[foo2, HoldFirst]
foo2[fun_] := Level[Map[HoldForm,Hold[fun],{2}],{2}]
x=2;y=3;
foo2[goo[x,y]]
{x,y}
HoldForm is invisible, but we can see it in FullForm
FullForm[%]
List[HoldForm[x],HoldForm[y]]
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565