3
$\begingroup$

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)

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

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]
$\endgroup$
2
  • $\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$ – Fidel I. Schaposnik 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.