TransWikia.com

Pattern Matching BlankSequence

Mathematica Asked on February 27, 2021

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?

One Answer

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}}

Correct answer by wuyudi on February 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP