8
$\begingroup$

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

$\endgroup$
  • $\begingroup$ I get {False, True, True, True} when I evaluate your code and that is what I would have expected to get. $\endgroup$ – Mike Honeychurch Apr 18 '12 at 22:26
  • $\begingroup$ @MikeHoneychurch sorry must've copied it down wrong! $\endgroup$ – tkott Apr 18 '12 at 22:28
10
$\begingroup$

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}
*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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