Re: Replace, test question

*To*: mathgroup at smc.vnet.net*Subject*: [mg120348] Re: Replace, test question*From*: Bill Rowe <readnews at sbcglobal.net>*Date*: Tue, 19 Jul 2011 06:59:29 -0400 (EDT)

On 7/18/11 at 6:13 AM, tio540s1 at gmail.com (PA32R) wrote: >I'm very new to Mathematica and trying to work my way through it. I >don't understand why: {58, 61, 15, 66, 10, 2, 24, 81, 45, 84} /. x_ >/; PrimeQ -> P returns: {58, 61, 15, 66, 10, 2, 24, 81, 45, 84} >I would expect it to take the list, replace everything in the list >that matches any pattern and passes the PrimeQ test, i.e., is prime, >with P to yield: {58, P, 15, 66, 10, P, 14, 81, 45, 84} >Can someone explain my misunderstanding? The way you have written things the test you intend to apply simply isn't applied. In essence you have told Mathematica call the pattern _ x then whenever PrimeQ (of nothing) is true replace that pattern with P. And since PrimeQ (of nothing) will never evaluate as True, Mathematica does no replacement The syntax you want is either In[1]:= {58, 61, 15, 66, 10, 2, 24, 81, 45, 84} /. x_ /; PrimeQ[x] -> P Out[1]= {58,P,15,66,10,P,24,81,45,84} or more concise would be to use PatternTest rather than Condition (/;), i.e., In[2]:= {58, 61, 15, 66, 10, 2, 24, 81, 45, 84} /. x_?PrimeQ -> P Out[2]= {58,P,15,66,10,P,24,81,45,84}