# UnsameQ exhibits different behavior than !SameQ

I do not understand the UnsameQ[] behavior below. (I need UnsameQ[] as I may have variables in the array.)

{-7, 3, 2} /. x_ /; x == 3 -> w
(*{-7, w, 2}*)

{-7, 3, 2} /. x_ /; x != 3 -> w
(*{w, 3, w}*)

{-7, 3, 2} /. x_ /; x === 3 -> w
(*{-7, w, 2}*)

{-7, 3, 2} /. x_ /; x =!= 3 -> w
(*w*)

• {a} /. _ -> "who said it works only at the first level?" and {} != 3 vs {} =!= 3.
– Kuba
Jan 23, 2019 at 14:37
• @Kuba, thank you for your reply. However, I do not fully understand. Apparently in the UnsameQ[] case, the entire {-7,3,2} is becoming x (/after/ the first level?) and not the same as 3. Why does that not continue with Unequal[]? (For that matter, I am a tad stymied why {}!=3 does not return False.) Jan 23, 2019 at 15:26
• ! SameQ[] would be {-7, 3, 2} /. x_ /; ! (x === 3) -> w, which returns the same as UnsameQ[]. But ! Equal[] is different than UnsameQ[], as your 2nd and 4th examples show. Jan 23, 2019 at 21:00
• Perhaps you want Replace[list, x_ /; x =!= 3 -> w, 1] or Replace[list, x_ /; x =!= 3 -> w, Infinity] Jan 23, 2019 at 21:01
• @Michael E2, thank you for your reply. Yes, I also played with !SameQ[]. I see that !Equal[] and UnsameQ[] are different, my questions are: why? and why should I have known that? Also, I would appreciate a pointer to some documentation, explaining why {}!=3 does not return False. Thank you. Jan 24, 2019 at 13:23

w != 3 returns unevaluated, thus no replacement will ever happen since it will never match.

/. is ReplaceAll, it happens at all levels. At level 0, {-7, 3, 2} =!= 3 is true, so the replacement is made and there are no more levels left to traverse.

Edit:

I suggest using replace with a level spec:

Replace[{-7, 3, 2}, x_ /; x =!= 3 -> w, {1}]

• Thank you. I'm okay with the Replace[] levels. Still trying to wrap my head around why {-7,3,2}!=3 is not True. Jan 24, 2019 at 15:59

As a diagnostic tool for these sorts of things, if you sneak in a Print[] statement you can see what is going on in all of your cases. For the =!= versus the != you can see the UnsameQ always evaluates to true or false while the != doesn't.

{-7, 3, 2} /. x_ /; (Print["x= ", x, " : ", x =!= 3]; x =!= 3) -> w


x= {-7,3,2} : True

Now the !=

{-7, 3, 2} /. x_ /; (Print["x= ", x, " : ", x != 3]; x != 3) -> w


x= {-7,3,2} : {-7,3,2}!=3

x= List : List!=3

x= -7 : True

x= 3 : False

x= 2 : True

• Thank you, I like this diagnostic use of Print[]. :-) What remains for me to understand is why {-7,3,2}!=3 does not simplify to True. I guess it is like @Michael E2 mentioned, that the two are not /numerically/ unequal, since a List[] is not numerical. If there is a better explanation, I am all ears, thanx. Jan 24, 2019 at 15:57
• Hi All. I still would like to understand why {1,2,3}==9 returns unevaluated. @Michael E2 had written something about the List[] not being NumericQ[]. (I can no longer find that comment.) But in that case: "asdf"==9 should also return unevaluated, since "asdf" is not NumericQ[] -- but it returns False. Any pointers would be most appreciated here. Thank you. (Should I open a new question?) Jan 29, 2019 at 20:37