1
$\begingroup$

I'm trying the following pattern matching command:

ReplaceList[Hold[b + a], Hold[x___ + y___] -> {{x}, {y}}]

And Mathematica returns:

{{{}, {b, a}}, {{b}, {a}}, {{a}, {b}}, {{b, a}, {}}}

Can someone help me to understand why {{}, {a, b}} and {{a, b}, {}} don't belong to the solution?

As Plus head has Orderless attribute, the pattern matcher should try all permutations (and this explains why {{}, {a, b}} belongs to the solution). Where am I wrong?

Thanks for your help.

$\endgroup$
  • 3
    $\begingroup$ Orderless means {a, b} and {b, a} are equivalent. They are (probably) considered identical in pattern matching to reduce combinatorial explosion of cases. $\endgroup$ – Michael E2 Dec 8 '18 at 12:03
2
$\begingroup$

Turning my comment into an answer:

Orderless means {a, b} and {b, a} are equivalent. They are (probably) considered identical in pattern matching to reduce combinatorial explosion of cases. So in terms of ReplaceList applying the replacement in "all possible ways", {a, b} and {b, a} are considered the same and only one result is returned in the list.

$\endgroup$

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.