1
$\begingroup$

This question already has an answer here:

I am trying to match expressions that are the sum of two or more objects with a specific head. However I can not get this work properly.

For example

obj[a]+obj[b] /. Plus[A:Repeated[_obj,{2,Infinity}] :> Hold[A] 

returns

obj[a]+obj[b]

instead of

Hold[obj[a]+obj[b]]

as desired.

Somewhat confusingly allowing single repeats

obj[a]+obj[b] /. Plus[A:Repeated[_obj,{1,Infinity}] :> Hold[A]

does return

Hold[obj[a]]+Hold[obj[b]]

as expected. Also using a different overall head instead of "Plus" for example "List" also works

List[obj[a],obj[b]] /. List[A:Repeated[_obj,{2,Infinity}] :> Hold[A] 

returns

Hold[obj[a],obj[b]]

Does anybody have an idea what is going on here (and how to achieve what I was trying in the first place)?

$\endgroup$

marked as duplicate by Mr.Wizard Apr 14 '16 at 11:23

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ Plus on the left-hand-side of the rule evaluates, which changes your pattern. Try obj[a] + obj[b] /. Verbatim[Plus][A : Repeated[_obj, {2, Infinity}]] :> Hold[A] instead. This question is a duplicate and I shall mark it as such in a minute. $\endgroup$ – Mr.Wizard Apr 14 '16 at 11:22
  • $\begingroup$ If anyone has a suggestion for a better duplicate please comment or flag. $\endgroup$ – Mr.Wizard Apr 14 '16 at 11:23