4

This is an example I'm reading that picks out the Primes, but I don't quite see how the _? works. I haven't seen it before and can't find it in Help.

Cases[Range[500], _?PrimeQ]
xzczd
  • 65,995
  • 9
  • 163
  • 468
cybervigilante
  • 599
  • 2
  • 7

1 Answers1

5

This is called conditional pattern-matching.

enter image description here

From Professor Richard J. Gaylord's Wolfram Language Fundamentals Part One at time 57:04

See also using-a-patterntest-versus-a-condition-for-pattern-matching

Here is the main page for documentation on conditional pattern matching PatternTest

To check/test the pattern before using it somewhere, use MatchQ

Nasser
  • 143,286
  • 11
  • 154
  • 359
  • " easily found in the documentation." That is not the case. All other shorthands, such as /. , /; , __, etc, are indeed found, but _? cannot be found. If I am in error please give the link. – cybervigilante Nov 15 '22 at 21:52
  • @cybervigilante it is not me who said easily found in the documentation that is in the message generated automatically by the system when question is closed. So I have nothing to do with the message. You can ask new/separate question if you want about this. – Nasser Nov 15 '22 at 21:56
  • My error. That was just a general reply to the sometime unhelpful administration, which I hurriedly mis-posted since I was annoyed at their attitude of closing down what I felt was a valid query. – cybervigilante Nov 15 '22 at 23:59
  • @cybervigilante no problem. If you want to complain about an issue related to administrative issue on this site, you can contact the admin or post question at meta site for Mathematica (link is above) – Nasser Nov 16 '22 at 00:02
  • 1
    @cybervigilante Notice that, as mentioned by thorimur in comment above, _? is actually combination of two functions Blank and PatternTest. By searching the single ? in document, you'll find PatternTest. When you see unknown shorthand in Mathematica, a easy way to find their full form is to use //Hold//FullForm e.g. Cases[Range[500], _?PrimeQ] // Hold // FullForm. – xzczd Dec 04 '22 at 07:46