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.

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, rm -rf 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

2 Answers 2

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

f[a]

f@{a}

f[{a}]

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.