Blank

or Blank[]
is a pattern object that can stand for any Wolfram Language expression.

or Blank[h]
can stand for any expression with head h.

DetailsDetails

  • The head h in cannot itself contain pattern objects.

Background
Background

  • Blank is a pattern object that can stand for any expression in the Wolfram Language. The expression Blank[] may be compactly denoted with the underscore character _. The more specific pattern Blank[h] (compactly denoted _h) stands for any expression with head h. Blank is an extremely powerful and useful construct for pattern-based programming and is the foundation upon which pattern-matching functionality is built. The most common function taking Blank and related patterns as arguments is SetDelayed. Other such functions include MatchQ, Cases, DeleteCases, Count, and RuleDelayed.
  • Sequences of one or more blanks are represented using the pattern object BlankSequence (double underscore: __). Sequences of zero or more blanks are represented using the pattern object BlankNullSequence (triple underscore: ___). In general, patterns involving multiple instances of Blank are faster to work with than those involving BlankSequence and BlankNullSequence, since vastly more possibilities must be tested for a possible match in the latter two cases.
  • Blank may be used together with Optional to specify a pattern object for an expression which, if omitted, is replaced by a default value. The Alternatives pattern object allows specification of several possible patterns, including Blank. Blank may be combined with a PatternTest (written in shorthand as p?test). For example, the code Cases[Range[100], _?(IntegerQ[Sqrt[#]] &)]] (which makes use of a pure function with a Slot specification) selects and returns the list of all square numbers less than or equal to 100.

ExamplesExamplesopen allclose all

Basic Examples  (2)Basic Examples  (2)

A pattern that matches any expression:

In[1]:=
Click for copyable input
Out[1]=

A pattern that matches any symbol:

In[2]:=
Click for copyable input
Out[2]=

A function definition that will take any single argument:

In[1]:=
Click for copyable input
In[2]:=
Click for copyable input
Out[2]=

A function definition for integer arguments only:

In[3]:=
Click for copyable input
In[4]:=
Click for copyable input
Out[4]=
Introduced in 1988
(1.0)