Mathematica 9 is now available
THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.
Virtual Book > Core Language > Patterns > Patterns Involving Alternatives >
MATHEMATICA TUTORIAL
|Related Tutorials »

Patterns Involving Alternatives

patt1|patt2|...a pattern that can have one of several forms

Specifying patterns that involve alternatives.

This defines to give when its argument is either or .
In[1]:=
Click for copyable input
The first two cases give .
In[2]:=
Click for copyable input
Out[2]=
You can also use alternatives in transformation rules.
In[3]:=
Click for copyable input
Out[3]=
Here is another example, in which one of the alternatives is itself a pattern.
In[4]:=
Click for copyable input
Out[4]=
When you use alternatives in patterns, you should make sure that the same set of names appear in each alternative. When a pattern like matches an expression, there will always be a definite expression that corresponds to the object . If you try to match a pattern like , then there still will be definite expressions corresponding to and , but the unmatched one will be Sequence.
Here is used to name the head, which can be either or .
In[5]:=
Click for copyable input
Out[5]=
Ask a question about this page  |  Suggest an improvement  |  Leave a message for the team
Format:   HTML  |  CDF