Why part is explained nicely in @ciao's answer. This post deals with the part:
I want to detect whether this list has repeated element a,a or b,b
You can get what you expected to get from Cases[...]
using the new-in-v-10 function SequenceCases
:
list={a,b,a,b,b,b,a,b,a,a,b};
SequenceCases[list,{Repeated[x_,{2}]}]
{{b, b}, {a, a}}
Or, define a Boolean
function that returns True
when the input list contains repeated elements:
containsRepeatedQ = SequenceCases[#,{Repeated[x_,{#2}]}] != {}&;
containsRepeatedQ[{a,b,a,b,a,b,a,b}, 2]
False
containsRepeatedQ[{a,b,a,b,a,b,a,a,b}, 2]
True
Cases[list, x_ /; (Print@x; True)]
and see what you get.Cases[Split@list, {Repeated[a | b, {2}]}]
will spit out a list of matches,MemberQ[Split@list, {Repeated[a | b, {2}]}]
will returnTrue
orFalse
if any are there (you said "detect", hence I'm guessing that's what you meant.) There are other ways, that should get you started. $\endgroup$