4
$\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
  • 1
    $\begingroup$ Interesting: f[Plus[a, b]] /. f[g_[l1__]] :> f[l1] gives: f[a, b] $\endgroup$ Commented Mar 8, 2021 at 12:50

1 Answer 1

11
$\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
  • 1
    $\begingroup$ you can also do f[a + b] /. f[l_Plus]] :> f[l] $\endgroup$ Commented Mar 8, 2021 at 13:16
  • 2
    $\begingroup$ That still evaluates to f[a + b], right? $\endgroup$ Commented Mar 8, 2021 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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