This is documentation for Mathematica 8, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
MATHEMATICA TUTORIAL
|Related Tutorials »

Specifying Types of Expression in Patterns

You can tell a lot about what "type" of expression something is by looking at its head. Thus, for example, an integer has head Integer, while a list has head List.
In a pattern, and represent expressions that are constrained to have head h. Thus, for example, represents any integer, while represents any list.
x_han expression with head h
x_Integeran integer
x_Realan approximate real number
x_Complexa complex number
x_Lista list
x_Symbola symbol

Patterns for objects with specified heads.

This replaces just those elements that are integers.
In[1]:=
Click for copyable input
Out[1]=
You can think of making an assignment for as like defining a function that must take an argument of "type" Integer.
This defines a value for the function when its argument is an integer.
In[2]:=
Click for copyable input
The definition applies only when the argument of is an integer.
In[3]:=
Click for copyable input
Out[3]=
The object has head Real, so the definition does not apply.
In[4]:=
Click for copyable input
Out[4]=
This defines values for expressions with integer exponents.
In[5]:=
Click for copyable input
The definition is used only when the exponent is an integer.
In[6]:=
Click for copyable input
Out[6]=