Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I can't seem to find the right set of patterns to independently match two different lists. Simplified versions of the lists are as follows:

list1 = {{2}, {2}, {20}, {2}, {10}}
list2 = {{2}, {2}, {2}, {2}, {2}, {2}}

In general, the second list will always have the same number inside the parentheses. They can have different lengths. Both will have just a single number inside the parentheses.

I am trying to find two patterns (p1,p2):

p1 = {{s_?NumericQ} ..};
p2 = {{_?NumericQ} ..};

Such that

{MatchQ[list1,p1],MatchQ[list1,p2],MatchQ[list2,p1],MatchQ[list2,p2]}

Gives

{True, False, False, True}

My test patterns give {True, True, False, True} instead. I'm sure the answer is easy, but I just can't get it right...

share|improve this question
    
I get {False, True, True, True} when I evaluate your code and that is what I would have expected to get. –  Mike Honeychurch Apr 18 '12 at 22:26
    
@MikeHoneychurch sorry must've copied it down wrong! –  tkott Apr 18 '12 at 22:28

1 Answer 1

up vote 10 down vote accepted

The following patterns give the required output:

p1 = p : {{_?NumericQ} ..} /; ! Equal @@ p
p2 = {{s_?NumericQ} ..};


MatchQ[#[[1]], #[[2]]] & /@ Tuples[{{list1, list2}, {p1, p2}}]

(*
==> {True, False, False, True}
*)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.