Wolfram Research

Function Repository Resource:

FindHeadArities

Source Notebook

Find the distinct heads that occur in an expression and their corresponding arities

Contributed by: Stephen Wolfram, Jan Mangaldan and Nikolay Murzin

ResourceFunction["FindHeadArities"][expr]

gives an association whose keys are the distinct heads in expr, and whose values are the arities of those heads.

Details and Options

ResourceFunction["FindHeadArities"] has the attributes HoldFirst and SequenceHold.
With the default option setting "IncludeAtoms"False, ResourceFunction["FindHeadArities"] includes only the heads of non-atomic expressions. With "IncludeAtoms"True, heads of atomic expressions are included.

Examples

Basic Examples

Find the arities of various heads in the given expression:

In[1]:=
ResourceFunction["FindHeadArities"][f[f[a, b], c] == g[f[b, c]]]
Out[1]=

Scope

FindHeadArities lists all the arities found for a head:

In[2]:=
ResourceFunction["FindHeadArities"][f[x, f[x, y, z], u, f[v, w]]]
Out[2]=

Options

IncludeAtoms

By default, FindHeadArities ignores atomic expressions:

In[3]:=
ResourceFunction["FindHeadArities"][b[] == c[a, b]]
Out[3]=

Use "IncludeAtoms"True to account for atomic expressions:

In[4]:=
ResourceFunction["FindHeadArities"][b[] == c[a, b], "IncludeAtoms" -> True]
Out[4]=

Possible Issues

FindHeadArities operates on the evaluated form of expressions:

In[5]:=
ResourceFunction["FindHeadArities"][(1 + 2 5) + 3 5 7]
Out[5]=
In[6]:=
ResourceFunction["FindHeadArities"][Sequence[2, 3]]
Out[6]=

Wrap expressions in Unevaluated to work with unevaluated versions:

In[7]:=
ResourceFunction["FindHeadArities"][Unevaluated[(1 + 2 5) + 3 5 7]]
Out[7]=
In[8]:=
ResourceFunction["FindHeadArities"][Unevaluated[Sequence[2, 3]]]
Out[8]=

Resource History

Related Resources

License Information