HoldPattern

HoldPattern[expr]
is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.

DetailsDetails

  • HoldPattern has attribute HoldAll.
  • The left-hand sides of rules are usually evaluated, as are parts of the left-hand sides of assignments. You can use HoldPattern to stop any part from being evaluated.

ExamplesExamplesopen allclose all

Basic Examples (4)Basic Examples (4)

Set up a pattern whose left-hand side is kept unevaluated:

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

Use the pattern:

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

Make a definition without the argument of being evaluated:

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

Log[a, b] autoevaluates to Log[b]/Log[a], so there is a match:

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

Cases[e, patt->rhs] finds elements that match patt; use HoldPattern to find rules:

In[1]:=
Click for copyable input
Out[1]=
New in 3
New to Mathematica? Find your learning path »
Have a question? Ask support »