This is documentation for Mathematica 4, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)
Wolfram Research, Inc.

Setting Up Functions with Optional ArgumentsVerbatim Patterns

2.3.11 Repeated Patterns

Repeated patterns.

Multiple blanks such as x__ allow you to give patterns in which sequences of arbitrary expressions can occur. The Mathematica pattern repetition operators .. and ... allow you to construct patterns in which particular forms can be repeated any number of times. Thus, for example, f[a..] represents any expression of the form f[a], f[a, a], f[a, a, a] and so on.

The pattern f[a..] allows the argument a to be repeated any number of times.

In[1]:= Cases[{ f[a], f[a, b, a], f[a, a, a] }, f[a..]]

Out[1]=

This pattern allows any number of a arguments, followed by any number of b arguments.

In[2]:= Cases[{ f[a], f[a, a, b], f[a, b, a], f[a, b, b] },

f[a.., b..]]

Out[2]=

Here each argument can be either a or b.

In[3]:= Cases[{ f[a], f[a, b, a], f[a, c, a] }, f[(a | b)..]]

Out[3]=

You can use .. and ... to represent repetitions of any pattern. If the pattern contains named parts, then each instance of these parts must be identical.

This defines a function whose argument must consist of a list of pairs.

In[4]:= v[x:{{_, _}..}] := Transpose[x]

The definition applies in this case.

In[5]:= v[{{a1, b1}, {a2, b2}, {a3, b3}}]

Out[5]=

With this definition, the second elements of all the pairs must be the same.

In[6]:= vn[x:{{_, n_}..}] := Transpose[x]

The definition applies in this case.

In[7]:= vn[{{a, 2}, {b, 2}, {c, 2}}]

Out[7]=

Setting Up Functions with Optional ArgumentsVerbatim Patterns