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
A list is List[...] internally
An atom reports its type name
The two-argument form wraps the result in f
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, orString.
Attributes: none registered.
References¶
See also: Real, Rational, Complex, Symbol, String
- Source:
src/part.c - Specification:
docs/spec/builtins/structural-manipulation.md - Tests:
tests/test_airybi.c - Tests:
tests/test_algebraicnumber.c - Tests:
tests/test_assoc_atomicity.c - Tests:
tests/test_autocompile.c
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.