When I execute f[g[a, b]] /. f[g[l__]] :> f[l] I get f[a, b] as expected.

When I execute f[a + b] /. f[Plus[l__]] :> f[l] I get f[a + b]. (I expect f[a, b] again)

Why? (Wolfram Mathematica 12.2.0)

  • $\begingroup$ Interesting: f[Plus[a, b]] /. f[g_[l1__]] :> f[l1] gives: f[a, b] $\endgroup$ Mar 8 at 12:50

This happens because Plus[l__] evaluates to l__ before the pattern is even replaced. Try instead:

f[a + b] /. f[HoldPattern@Plus[l__]] :> f[l]
  • $\begingroup$ you can also do f[a + b] /. f[l_Plus]] :> f[l] $\endgroup$ Mar 8 at 13:16
  • 2
    $\begingroup$ That still evaluates to f[a + b], right? $\endgroup$ Mar 8 at 13:20

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.