Re: Replace, test question
- To: mathgroup at smc.vnet.net
- Subject: [mg120350] Re: Replace, test question
- From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
- Date: Tue, 19 Jul 2011 06:59:51 -0400 (EDT)
- References: <j0113e$qad$1@smc.vnet.net>
Here are two syntactic forms with essentially the same meaning in this
context (and which give the desired output):
{58, 61, 15, 66, 10, 2, 24, 81, 45, 84} /. x_?PrimeQ -> P
{58, 61, 15, 66, 10, 2, 24, 81, 45, 84} /. x_ /; PrimeQ[x] -> P
You seem to have a hybrid of these, which itself has a different meaning:
for each x, PrimeQ is evaluated; since PrimeQ on its own evaluates to
itself, the pattern does not match anywhere.
On Mon, 18 Jul 2011 11:15:10 +0100, PA32R <tio540s1 at gmail.com> 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?
>
> Thanks.