Skip to content

Head

Status: Stable

documented, exercised by the test suite and/or worked examples, with no known limitations recorded.

Description

Head[expr]

gives the head of expr.

Head[expr, h]

wraps the result with h, i.e. returns h[Head[expr]].

Notes For atoms, Head returns Integer, Real, BigInt, Rational, Complex, Symbol, or String; for a compound expression f\[...\], Head returns f.

Examples (7)

Every input below was run against the current Mathilda build and its output recorded.

Basic examples (3)

In[1]:= Head[f[x]]
Out[1]= f

In[2]:= Head[3/4]
Out[2]= Rational

In[3]:= Head[a + b, f]
Out[3]= f[Plus]

Applications (4)

The symbol applied as the head

In[4]:= Head[f[x]]
Out[4]= f

A list is List[...] internally

In[5]:= Head[{1, 2, 3}]
Out[5]= List

An atom reports its type name

In[6]:= Head[3/4]
Out[6]= Rational

The two-argument form wraps the result in f

In[7]:= Head[a + b, f]
Out[7]= f[Plus]

Implementation notes

builtin_head (in src/part.c) returns the head of its argument via expr_head — f for f[...], and the type symbol (Integer, Symbol, List, etc.) for atoms. The 2-arg form Head[expr, h] wraps the result as h[Head[expr]], leaving the outer application for the evaluator to reduce.

  • For functions, returns the symbol or expression acting as the head.
  • For atoms, returns the symbolic type name: Integer, Real, Rational, Complex, Symbol, or String.

Attributes: none registered.

References

See also: Real, Rational, Complex, Symbol, String

Notes & additional examples

Notes

Head[expr] returns the top-level wrapper via expr_head: the applied symbol or expression for a function (f for f[x], Plus for a + b, List for a list), and the type symbol for an atom — Integer, Real, Rational, Complex, Symbol, or String. The two-argument form Head[expr, h] returns h[Head[expr]], leaving the outer application for the evaluator to reduce. Head extraction is also what expr[[0]] and Extract[expr, {0}] perform.