# Introduction to Patterns

Patterns are used throughout the Wolfram Language to represent classes of expressions. A simple example of a pattern is the expression . This pattern represents the class of expressions with the form f[anything].

The main power of patterns comes from the fact that many operations in the Wolfram Language can be done not only with single expressions, but also with patterns that represent whole classes of expressions.

In[1]:= |

Out[1]= |

In[2]:= |

Out[2]= |

The basic object that appears in almost all Wolfram Language patterns is (traditionally called "blank" by Wolfram Language programmers). The fundamental rule is simply that _ *stands for any expression*. On most keyboards the underscore character appears as the shifted version of the dash character.

Thus, for example, the pattern stands for any expression of the form f[anything]. The pattern also stands for any expression of the form f[anything], but gives the name to the expression anything, allowing you to refer to it on the right‐hand side of a transformation rule.

You can put blanks anywhere in an expression. What you get is a pattern which matches all expressions that can be made by "filling in the blanks" in any way.

f[n_] | with any argument, named |

f[n_,m_] | with two arguments, named and |

x^n_ | to any power, with the power named |

x_^n_ | any expression to any power |

a_+b_ | a sum of two expressions |

{a1_,a2_} | a list of two expressions |

f[n_,n_] | with two identical arguments |

In[3]:= |

Out[3]= |

One of the most common uses of patterns is for "destructuring" function arguments. If you make a definition for , then you need to use functions like Part explicitly in order to pick out elements of the list. But if you know for example that the list will always have two elements, then it is usually much more convenient instead to give a definition instead for . Then you can refer to the elements of the list directly as and . In addition, the Wolfram Language will not use the definition you have given unless the argument of really is of the required form of a list of two expressions.

In[4]:= |

In[5]:= |

A crucial point to understand is that Wolfram Language patterns represent classes of expressions with a given *structure*. One pattern will match a particular expression if the structure of the pattern is the same as the structure of the expression, in the sense that by filling in blanks in the pattern you can get the expression. Even though two expressions may be *mathematically equal*, they cannot be represented by the same Wolfram Language pattern unless they have the same structure.

Thus, for example, the pattern can stand for expressions like or that have the same *structure*. However, it cannot stand for the expression . Although this expression is *mathematically equal* to , it does not have the same *structure* as the pattern .

The fact that patterns in the Wolfram Language specify the *structure* of expressions is crucial in making it possible to set up transformation rules which change the *structure* of expressions, while leaving them mathematically equal.

It is worth realizing that in general it would be quite impossible for the Wolfram Language to match patterns by mathematical, rather than structural, equivalence. In the case of expressions like and , you can determine equivalence just by using functions like Expand and Factor. But, as discussed in "Reducing Expressions to Their Standard Form" there is no general way to find out whether an arbitrary pair of mathematical expressions are equal.

As another example, the pattern will match the expression . It will not, however, match the expression , even though this could be considered as . "Optional and Default Arguments" discusses how to construct a pattern for which this particular case will match. But you should understand that in all cases pattern matching in the Wolfram Language is fundamentally structural.

In[6]:= |

Out[6]= |

Another point to realize is that the structure the Wolfram Language uses in pattern matching is the full form of expressions printed by FullForm. Thus, for example, an object such as , whose full form is Power[x,-1] will be matched by the pattern , but not by the pattern , whose full form is Times[x_,Power[y_,-1]]. Again, "Optional and Default Arguments" will discuss how you can construct patterns which can match all these cases.

In[7]:= |

Out[7]= |

In[8]:= |

Out[8]//FullForm= | |

Although the Wolfram Language does not use mathematical equivalences such as when matching patterns, it does use certain structural equivalences. Thus, for example, the Wolfram Language takes account of properties such as commutativity and associativity in pattern matching.

In[9]:= |

Out[9]= |

The discussion considers only pattern objects such as which can stand for any single expression. Other tutorials discuss the constructs that the Wolfram System uses to extend and restrict the classes of expressions represented by patterns.