Mathematica 9 is now available
 Documentation / Mathematica / Built-in Functions / Programming / Pattern Matching  /
PatternTest

  • p ? test is a pattern object that stands for any expression which matches p, and on which the application of test gives True.
  • Any result for test [ pval ] other than True is taken to signify failure.
  • Example: _?NumberQ represents a number of any type. The _ matches any expression, and ?NumberQ restricts to any expression which gives True on application of the number test NumberQ.
  • The operator ? has a high precedence. Thus _^_?t is _^(_?t) not (_^_)?t.
  • In a form such as __? test every element in the sequence matched by __ must yield True when test is applied.
  • See the Mathematica book: Section 2.3.5.
  • See also: Condition.

    Further Examples

    Here are two definitions.

    In[1]:=

    In[2]:=

    The definition for p1 applies only when x is manifestly a number.

    In[3]:=

    Out[3]=

    The definition for p2 is more liberal.

    In[4]:=

    Out[4]=

    In[5]:=



    Any questions about topics on this page? Click here to get an individual response.Buy NowMore Information
    THIS IS DOCUMENTATION FOR AN OBSOLETE PRODUCT.
    SEE THE DOCUMENTATION CENTER FOR THE LATEST INFORMATION.