Pattern  (x:pattn)

Mathematica lets us use (x_Foo) to say that something with the head Foo will be  called (x).  It also lets us use (x_?Test)  to say something for which Test  returns True will be called (x).  Now if we want to say that something  matching a certain pattern is called (x) we use (x : pattn).  


The next cell gives an example where use of Pattern is helpful.  Here (ff) is
only defined when it's argument is an Integer raised to a power.  In addition
the exponent must have the head Plus.

ff[x : (_Integer^_Plus)] := Thread[x, Plus]

In the example below we get the result of evaluating  Thread[2^(3 x - 5y), Plus].

ff[2^(3x - 5y)]

2^(3 x) + 2^(-5 y)


Next we see (ff) is undefined when it's argument isn't an Integer raised to a
power where the exponent has the head Plus.

ff[2^(3x)]

ff[2^(3 x)]

ff[x^(3x - 5y)]

ff[x^(3 x - 5 y)]


Created by Mathematica  (May 16, 2004)

Back to Ted’s Tricks index page