Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Why doesn’t PatternTest work with Composition?

I'd like my function to only evaluate when the argument is a list of pairs. It seems like Repeated and a question mark should work:

fourPears[argument_?MatchQ[#, {{_, _} ..}] &]

but it does not. What's wrong with this?

share|improve this question

marked as duplicate by Leonid Shifrin, R. M. Dec 10 '12 at 21:09

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.

The question mark is called PatternTest in this context and is not to be confused with Information, which also has a question mark as its short form. – ArgentoSapiens Dec 10 '12 at 23:22
up vote 2 down vote accepted

I prefer the simpler pattern pairs : {{_, _} ..}. For example,

f[pairs : {{_, _} ..}] := Row[{pairs, " is a list of pairs"}]





f@{a, b, {c, d}}

f[{a, b, {c, d}}]

f@{{a, w}, {b, x}, {c, y}}

{{a, w}, {b, x}, {c, y}} is a list of pairs

share|improve this answer

The ? in this expression (does it have a name?) has high precedence, so it sticks to argument_ and MatchQ more than MatchQ sticks to its own arguments. You need parentheses.

fourPears[argument_?(MatchQ[#, {{_, _} ..}] &)]

should work, but it might not be the best way to accomplish this.

share|improve this answer

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