0
$\begingroup$

Consider the definition:

Clear[fun]
fun[a_ + b_] := fun[a] + fun[b];
fun[a+b+c]
(*fun[a] + fun[b] + fun[c]*)

This works as expected. However, if we use a BlankSequence on "b":

Clear[fun]
fun[a_ + b__] := fun[a] + fun[b];
fun[a + b + c]
(*fun[a] + fun[b, c]*)

it seems that BlankSequence eliminates the Plus in "b+c" and replaces it with Sequence. Has anybody a good explanation for this behaviour?

$\endgroup$
2
$\begingroup$

a+b+c == Plus[a,b,c]

So fun[a_ + b__] == fun[Plus[a_, b__]]

when input a + b + c,which equals Plus[a,b,c] , mma match b with b,c

a + b + c /. a_ + b__ :> {a, {b}}

{a, {b, c}}

$\endgroup$
3
  • $\begingroup$ Hi, here a follow up question: Can you explain why the "b" in the following expression gets lost?: Clear[fu] fu[a_ + b__] := fu[a] + fu[(Print[FullForm@Hold@b]; b)]; fu[a + b + c] $\endgroup$ – Daniel Huber Oct 5 '20 at 8:59
  • $\begingroup$ @DanielHuber I tried these 2 code: a + b + c /. a_ + b__ :> {{a}, {None; b}} and a + b + c /. a_ + b__ :> {{a}, {b}} , find something interesting. I am not able to explain it. $\endgroup$ – wuyudi Oct 5 '20 at 10:00
  • $\begingroup$ HI, the same happens with any symbol you like. E.g.a + b + c /. a_ + b__ :> {{a}, {gaga; b}} There are some mysteries with BlankSequence that I do not understand $\endgroup$ – Daniel Huber Oct 5 '20 at 10: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.