The documentation says that "Rule represents a rule that transforms lhs to rhs" and gives the following example:

{x, x^2, x^3, a, b} /. x^n_ -> f[n]

(*{x, f[2], f[3], a, b}*)

I suppose, however, it should have given {f[1], f[2], f[3], a, b} and further tried a more explicit specification:

{x^1, x^2, x^3, a, b} /. x^n_ -> f[n]

As you may see, the output remains unchanged! What is the explanation for the result for the first item?

  • $\begingroup$ Can you provide a link to the documentation page with the example (by editing your question, not here in comments)? For instance, for the text "The documentation says" (use Ctrl + L). $\endgroup$ – Peter Mortensen Sep 13 '19 at 10:03
  • $\begingroup$ @PeterMortensen I added the link. BTW thanks for showing the shortcut. $\endgroup$ – Αλέξανδρος Ζεγγ Sep 13 '19 at 13:21

Pattern matching is performed based on the form of an expression, not its (mathematical) meaning. Using jargon, pattern matching is performed syntactically, not semantically.

Specifically, here x^1 is evaluated/simplified first to be x. The latter no longer has the form of "one thing raised to the power of another thing", so it will not be matched when applied the rule afterward.

Consequently, if you want the first one to be matched, you can stop it from evaluation by using Hold or HoldForm and ReleaseHold in the end.

{HoldForm[x^1], x^2, x^3, a, b} /. x^n_ -> f[n] // ReleaseHold
{f[1], f[2], f[3], a, b}

To see the function of ReleaseHold, evaluate

{HoldForm[x^1], x^2, x^3, a, b} /. x^n_ -> f[n] // InputForm

and you obtain

{HoldForm[f[1]], f[2], f[3], a, b}

You see that HoldForm is still there, but usually in the OutputForm it is invisible and ReleaseHold can remove it.

Another more concise way is to use Default in the pattern (the dot following the underscore):

{x, x^2, x^3, a, b} /. x^n_. -> f[n]
{f[1], f[2], f[3], a, b}
  • $\begingroup$ Thanks, why add ReleaseHold? I found without it the result is just like what is desired. $\endgroup$ – user55777 Sep 12 '19 at 11:24
  • $\begingroup$ @user55777 Because the "side-effect" of the two hold functions should be neutralized. Please keep in mind that what you see in a Mathematica notebook may not what they look like. See my updates in the answer. $\endgroup$ – Αλέξανδρος Ζεγγ Sep 12 '19 at 12:07
  • $\begingroup$ I think the blank-dot syntax is much more preferred in this case, then to fiddle around with Hold and Release… $\endgroup$ – SHuisman Sep 12 '19 at 14:53
  • $\begingroup$ @SHuisman Yes, conspicuously! $\endgroup$ – Αλέξανδρος Ζεγγ Sep 12 '19 at 15:30
  • 1
    $\begingroup$ @SHuisman But fiddling with hold and release helps clarify some mechanism for pattern matching, doesn’t it? $\endgroup$ – Αλέξανδρος Ζεγγ Sep 12 '19 at 16:30

My recommendation is to look at the FullForm of the expression and the replacement.

FullForm[{x^1, x^2, x^3, a, b}]

List[x, Power[x,2], Power[x,3], x, b]



Power[x, Pattern[n, Blank[]]]

This makes it clear that the first element of the list doesn't match the pattern.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.