This is documentation for Mathematica 3, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
 Documentation / Mathematica / The Mathematica Book / Principles of Mathematica / Patterns  /

2.3.1 Introduction

Patterns are used throughout Mathematica to represent classes of expressions. A simple example of a pattern is the expression f[x_]. 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 Mathematica can be done not only with single expressions, but also with patterns that represent whole classes of expressions.

  • You can use patterns in transformation rules to specify how classes of expressions should be transformed.
  • In[1]:= f[a] + f[b] /. f[x_] -> x^2


  • You can use patterns to find the positions of all expressions in a particular class.
  • In[2]:= Position[{f[a], g[b], f[c]}, f[x_]]


    The basic object that appears in almost all Mathematica patterns is _ (traditionally called "blank" by Mathematica 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 f[_] stands for any expression of the form f[anything]. The pattern f[x_] also stands for any expression of the form f[anything], but gives the name x 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.

    Some examples of patterns.

  • You can construct patterns for expressions with any structure.
  • In[3]:= f[{a, b}] + f[c] /. f[{x_, y_}] -> p[x + y]


    One of the most common uses of patterns is for "destructuring" function arguments. If you make a definition for f[list_], 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 f[{x_,y_}]. Then you can refer to the elements of the list directly as x and y. In addition, Mathematica will not use the definition you have given unless the argument of f really is of the required form of a list of two expressions.

  • Here is one way to define a function which takes a list of two elements, and evaluates the first element raised to the power of the second element.
  • In[4]:= g[list_] := Part[list, 1] ^ Part[list, 2]

  • Here is a much more elegant way to make the definition, using a pattern.
  • In[5]:= h[{x_, y_}] := x ^ y

    A crucial point to understand is that Mathematica 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 Mathematica pattern unless they have the same structure.
    Thus, for example, the pattern (1+x_)^2 can stand for expressions like (1+a)^2 or (1+b^3)^2 that have the same structure. However, it cannot stand for the expression 1+2a+a^2. Although this expression is mathematically equal to (1+a)^2, it does not have the same structure as the pattern (1+x_)^2.
    The fact that patterns in Mathematica 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 Mathematica to match patterns by mathematical, rather than structural, equivalence. In the case of expressions like (1+a)^2 and 1+2a+a^2, you can determine equivalence just by using functions like Expand and Factor. But, as discussed in Section 2.5.2 there is no general way to find out whether an arbitrary pair of mathematical expressions are equal.
    As another example, the pattern x^_ will match the expression x^2. It will not, however, match the expression 1, even though this could be considered as x^0. Section 2.3.9 will discuss how to construct a pattern for which this particular case will match. But you should understand that in all cases pattern matching in Mathematica is fundamentally structural.

  • The x^n_ matches only x^2 and x^3. 1 and x can mathematically be written as

    , but do not have the same structure.
  • In[6]:= {1, x, x^2, x^3} /. x^n_ -> r[n]


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

  • The expressions in the list contain explicit powers of b, so the transformation rule can be applied.
  • In[7]:= {a/b, 1/b^2, 2/b^2} /. b^n_ -> d[n]


  • Here is the full form of the list.
  • In[8]:= FullForm[{a/b, 1/b^2, 2/b^2}]


    Although Mathematica does not use mathematical equivalences such as when matching patterns, it does use certain structural equivalences. Thus, for example, Mathematica

    takes account of properties such as commutativity and associativity in pattern matching.

  • To apply this transformation rule, Mathematica makes use of the commutativity and associativity of addition.
  • In[9]:= f[a + b] + f[a + c] + f[b + d] /.
    f[a + x_] + f[c + y_] -> p[x, y]


    The discussion so far has considered only pattern objects such as x_ which can stand for any single expression. In later subsections, we discuss the constructs that Mathematica uses to extend and restrict the classes of expressions represented by patterns.