Blank (_), BlankSequence (__), BlankNullSequence (___)

Blank (_), BlankSequence (__) and BlankNullSequence (___) are the most primative of Mathematica's pattern matching features.

Basic Blank (_) usage

In the first example Blank (_) is used for individual arguments.  In this case (f) is only defined when it's given two arguments.

Clear[f] ; f[x_, y_] := {x + 1, y + 1} {f[t, 3], f[t, 3, 5], f[]}

{{1 + t, 4}, f[t, 3, 5], f[]}

Basic BlankSequence (__) usage

In the next example BlankSequence (__) is used and (x___) represents a sequence of one or more arguments.  So in this case (f) is defined when it's given one or more argument, but undefined when given no arguments.

Clear[f] ; f[x__] := {0, x, 0} {f[t], f[t, 3, 5], f[]}

{{0, t, 0}, {0, t, 3, 5, 0}, f[]}

Basic BlankNullSequence (___) usage

In the next example BlankNullSequence(___) is used and (x___) refers to a sequence of any number of arguments, or an empty set of arguments.  In this case f[] returns {0,0} while (f) was undefined in the previous example.

Clear[f] ; f[x___] := {0, x, 0} {f[t], f[t, 3, 5], f[]}

{{0, t, 0}, {0, t, 3, 5, 0}, {0, 0}}

Use of Blanks without naming a variable

If a function doesn't use some of the arguments, it's more efficient to not name them.
In the following example I define a function that returns True when given any single argument.  Often times we will give a function other definitions to use in other cases, and make it use a catch all definition such as this in all other cases.

Clear[f] ; f[_] := True {f[t], f[t, 3, 5], f[]}

{True, f[t, 3, 5], f[]}

In the next cell (f) returns True when given one or more argument.

Clear[f] ; f[__] := True {f[t], f[t, 3, 5], f[]}

{True, True, f[]}

In the next cell (f) returns True when given any number of arguments or no arguments at all.

Clear[f] ; f[___] := True {f[t], f[t, 3, 5], f[]}

{True, True, True}

A more common use of an unamed BlankNullSequence is in the next cell.  In this case (f) is defined when it gets two or more arguments.  The first argument goes with (x_), the second argument goes with (y_), and all other arguments go with (___).

Clear[f] ; f[x_, y_, ___] := {x + 1, y + 1} {f[t], f[t, 3], f[u, 4, v, b, n, m]}

{f[t], {1 + t, 4}, {1 + u, 5}}

Use of named Blanks with a specified head

In the next example (f) is only defined when given two arguments that are symbols.

Clear[f, s, t] ; f[x_Symbol, y_Symbol] := {x + 1, y + 1} {f[s, t], f[s, 3], f[s, s + t]}

{{1 + s, 1 + t}, f[s, 3], f[s, s + t]}

In the next example (x__Real) represents a sequence of one or more arguments that are symbols.  If any argument of (f) is not a symbol, (f) is not defined.

Clear[f, s, t] ; f[x__Symbol] := Plus[x, 1] {f[s], f[s, t], f[], f[s, t, x + y]}

{1 + s, 1 + s + t, f[], f[s, t, x + y]}

In the next example BlankNullSequence(___) is used and (x___Symbol) refers to a sequence of any number of arguments with the head symbol, or an empty set of arguments.

Clear[f, s, t] ; f[x___Symbol] := Plus[x, 1] {f[s], f[s, t], f[], f[s, t, x + y]}

{1 + s, 1 + s + t, 1, f[s, t, x + y]}

Use of unamed Blanks with a specified head

In the next few examples there is no advantage in naming the arguments, and the definition evaluated faster than one where the argument is named.  In the next cell (f) returns True when given a single argument with the Head Real.

Clear[f] ; f[_Real] := True {f[2.3], f[2.3, 4.5], f[2.3, 3.4, 4.5], f[], f[4]}

{True, f[2.3, 4.5], f[2.3, 3.4, 4.5], f[], f[4]}

In the next cell (f) returns True when given one or more argument and all the arguments have the Head Real.

Clear[f] ; f[__Real] := True {f[2.3], f[2.3, 4.5], f[2.3, 3.4, 4.5], f[], f[4]}

{True, True, True, f[], f[4]}

In the next cell (f) returns True when given any number of arguments or no arguments at all, but only when all arguments given have the Head Real.

Clear[f] ; f[___Real] := True {f[2.3], f[2.3, 4.5], f[2.3, 3.4, 4.5], f[], f[4]}

{True, True, True, True, f[4]}


Created by Mathematica  (May 16, 2004)