# The difference between ? and /; [duplicate]

I can do the following

Cases[Range@10, x_ /; OddQ[x]]


But why cannot I do x_?OddQ[x] instead of _?OddQ in the following?

Cases[Range@10, x_ ? OddQ[x]]


Generally speaking, I am confused with using ? and /;.

# Edit

After reading the existing comments and answer, I understood now that ? needs function. However, it still confused me, when we prefer ? to /; and vice versa?

• I don't really understand the question. Did you read the documentation? ? must be followed by a function and OddQ[x] is not a function. This question feels a bit like "I can compute a sum with Sum, but why can't I compute a sum with Product?" – Szabolcs Sep 2 at 8:00
• PatternTest >> Properties and relations: "PatternTest applies test functions to patterns, which need not have names" and "Condition evaluates a Boolean expression on named parts of a pattern" – kglr Sep 2 at 8:10
• possible duplicate: Using a PatternTest versus a Condition for pattern matching – kglr Sep 2 at 8:20
• @kglr: Thank you. It is a duplicate by accident. – Money Sets You Free Sep 2 at 8:29

If you should use OddQ[x]

Cases[Range@10, x_?(x \[Function] OddQ[x])]


## Edit

Thanks to the comment for pointing it out, the x in (x \[Function] OddQ[x]) is irrelevant to the first x, you can replace it with any other vars.

In other words, (x \[Function] f[x]) == Function[x,f[x]] == f[#]& == f

• This is basically identical to what @Montevideo showed (use a pure function), but I find this version to be potentially misleading: you use x for two entire separate an unrelated purposes. Someone who is already confused about the difference between /; and ? could very well end up being even more confused, and believing that there is some connection between those two xs. – Szabolcs Sep 2 at 8:52

You need to do this to solve this problem:

Cases[Range@10, x_?(OddQ[#] &)]

Cases[Range@10, x_?(OddQ)]


You may have confused pattern matching and conditional judgment functions.

• (OddQ[#] &) is OddQ , nearly no difference. – wuyudi Sep 2 at 8:11
• Cases[Range[10], _?OddQ] – cvgmt Sep 2 at 9:06