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$
1
  • 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
    Commented Dec 8, 2018 at 12:03

1 Answer 1

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 and acknowledge you have read our privacy policy.

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