Template to match a list of matching items

I am looking for a template that matches a (possibly empty) list consisting of identical (in the sense of equal []) atomic objects, but I cannot figure it out. Any help would be greatly appreciated.

+7
source share
3 answers

All the answers so far seem to have missed the requirement that the matched objects be atomic . The following does the following:

Cases[testList, {a___?AtomQ} /; Equal[a]] 

If you do not define identity in the sense of Equal , you could use:

 Cases[testList, {(a_?AtomQ) ...}] 

With a slightly modified test list, you will see that other methods do not meet the requirements

 testList = {{1, 1.0, 1.0}, {a, b, c}, {Exp[Pi] + 1, Exp[Pi] + 1, Exp[Pi] + 1}, {}, {3}}; 

they all incorrectly match the third element.

+12
source

Does this work for you?

 testList = { {1, 1.0, 1.}, {a, b, c}, {0, Exp[Pi*I] + 1.0, Sin[Pi]} } Cases[testList, _List?(Equal @@ # &)] 
+9
source

Using Condition instead of PatternTest :

 In[31]:= testList = {{1, 1.0, 1.}, {a, b, c}, {0, Exp[Pi*I] + 1.0, Sin[Pi]}, {}, {3}}; Cases[testList, {a___} /; Equal[a]] Out[32]= {{1, 1., 1.}, {0, 0., 0}, {}, {3}} 

(and extends to Mark a list of test cases to cover empty and single lists.)

+6
source

All Articles