This is documentation for Mathematica 5, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.1)

Documentation / Mathematica / Built-in Functions / Programming / Pattern Matching /

Further Examples: Condition (/;)

To illustrate Condition we define RealRoots which, if the discriminant disc is nonnegative, returns the roots of a quadratic polynomial with real coefficients a, b, c. However, if disc is negative the calculation of the roots is blocked.

In[1]:=

Here disc is negative.

In[2]:=

Out[2]=

RealRoots returns unevaluated in this case.

In[3]:=

Out[3]=

Here RealRoots returns evaluated.

In[4]:=

Out[4]=

In[5]:=

Out[5]=

Here Condition is used to define a function depending on the sign of the argument.

In[6]:=

In[7]:=

In[8]:=

Here Condition is used to restrict the argument of randomRelativelyPrime to be a prime greater than . Given a prime p, randomRelativelyPrime picks a random number between and p-1 that is relatively prime to p-1.

In[9]:=

In[10]:=

Out[10]=

In[11]:=

Out[11]=

In[12]:=