Group Abstract Group Abstract

Message Boards Message Boards

1
|
5.6K Views
|
10 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Arities of function symbols

Posted 10 years ago

Given a symbol F used in function definitions like so :

F[x_,y_,z_] := ... ; F[x_,y_] := ... ; F[x_] := ... ; F := ...

Is it possible to introspect the system and get the various arities of that function symbol, something like :

Arities[F] -> {3,2,1,0}

Where -> denotes the evaluation of the expression.

Thanks for your help.

POSTED BY: Fabien Todescato
10 Replies
POSTED BY: Sander Huisman

I meant; I'm not sure how you can circumvent this behaviour ;)

POSTED BY: Sander Huisman

Yes... But having a lot of functions to try against numerous lists, an indexing scheme based on the arities of the functions would avoid incurring the cost of pattern matching in case the arity and the length of the list do not match... That was the idea... :)

POSTED BY: Fabien Todescato

I'm not sure if that is really necessary in the Wolfram Language;

if you just have a function definitions like:

f[x_] := x^2

and you try to evaluate f[2,3] or f[1,2,3] or f[], it will just remain unevaluated; i.e. it automatically checks if the number of arguments matches...

POSTED BY: Sander Huisman
POSTED BY: Sander Huisman

A more in-depth version would be something like:

ClearAll[Arities]
Arities[g_]:=Module[{dv,bl,blns,bls,opts,num},
    dv=DownValues[g];
    dv=dv[[All,1]];
    bl=Count[#,Verbatim[Blank[]],\[Infinity]]&/@dv;
    bls=Count[#,Verbatim[BlankSequence[]],\[Infinity]]&/@dv;
    blns=Count[#,Verbatim[BlankNullSequence[]],\[Infinity]]&/@dv;
    opts=Count[#,Verbatim[Optional][_,_],\[Infinity]]&/@dv;
    num=MapThread[#1+If[#2>0,#2 {1,\[Infinity]},0]+If[#3>0, #3{0,\[Infinity]},0]-#4{1,0}&,{bl,bls,blns,opts}];
    {dv,bl,bls,blns,opts,num}
]

ClearAll[F]
F[x_, y_, z_, f_, g_: 2, x2_: 3] := 14
F[x_, y_, z_, f_, g_: 2] := 14
F[x_, z_, f_, y___] := 14
F[x_, z_, y__] := 14
F[x_, y_] := 14
F[x_] := 23
F[] := 12

Arities[F] // Transpose // Grid

returns:

enter image description here

Which is a list of Definition, # of Blanks, # of BlankSequences, # of BlankNullSequenced, # of optional arguments, and range of number of arguments.

May I ask why you need the arities of the function? I can't really think of a use in 'normal' calculations...

POSTED BY: Sander Huisman
POSTED BY: Fabien Todescato
POSTED BY: Sander Huisman

Thank you Sander for your detailed explanations. I will need some time to study and digest your code, though.

I am interested in function arities in the context of an algorithm that tentatively applies function symbols to lists... Knowing beforehand the arities of the functions would allow me to use some indexing scheme so as to avoid applying functions of arities n to argument lists of length n' != n. In my case, the functions do not rely on sequence patterns, and have arities known beforehand.

POSTED BY: Fabien Todescato

Thank you so much Sanders, for your much appreciated help. Being quite new to the language, I am not that familiar with the notion of DownValues... or UpValues for that matter. I will have check the documentations.

Again, thank you.

POSTED BY: Fabien Todescato
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard