PatternTest
(what the ?
is shorthand for) requires a function. That function will be applied to the arguments supplied by whatever pattern matching situation is occurring. Condition
(what the /;
is shorthand for) requires an expression that (presumably) evaluates to True
or False
when evaluated with replacements being supplied by the pattern matching. What I gave previously was actually a bit over-specified. I should have suggested this:
Replace[{1, 2, 1/3, 3, 4, 1/5}, _?(Not@*IntegerQ) -> w, 1]
Notice I removed the x
as the name of the pattern, because it's never referred to in the rest of the pattern. Another way to do it if you don't like Composition
would be:
Replace[{1, 2, 1/3, 3, 4, 1/5}, _?(Not[IntegerQ[#]] &) -> w, 1]
That also works if you don't like spelling out Not
:
Replace[{1, 2, 1/3, 3, 4, 1/5}, _?(! IntegerQ[#] &) -> w, 1]
Regardless, you need a function, i.e. a thing that can be applied to arguments.
If you want to use Condition
instead of PatternTest
, then you don't use a "pure" function but an expression:
Replace[{1, 2, 1/3, 3, 4, 1/5}, x_ /; Not[IntegerQ[x]] -> w, 1]
The same explanation applies to Gianluca's suggestion:
(* Condition *)
{1, 2, 1/3, 3, 4, 1/5} /. x_ /; NumericQ[x] && ! IntegerQ[x] -> w
(* PatternTest *)
{1, 2, 1/3, 3, 4, 1/5} /. _?(NumericQ[#] && ! IntegerQ[#] &) -> w