1
$\begingroup$

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*)
$\endgroup$
  • 3
    $\begingroup$ {a} /. _ -> "who said it works only at the first level?" and {} != 3 vs {} =!= 3. $\endgroup$ – Kuba Jan 23 '19 at 14:37
  • $\begingroup$ @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.) $\endgroup$ – Aharon Naiman Jan 23 '19 at 15:26
  • $\begingroup$ ! 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. $\endgroup$ – Michael E2 Jan 23 '19 at 21:00
  • $\begingroup$ Perhaps you want Replace[list, x_ /; x =!= 3 -> w, 1] or Replace[list, x_ /; x =!= 3 -> w, Infinity] $\endgroup$ – Michael E2 Jan 23 '19 at 21:01
  • $\begingroup$ @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. $\endgroup$ – Aharon Naiman Jan 24 '19 at 13:23
6
$\begingroup$

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}]

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you. I'm okay with the Replace[] levels. Still trying to wrap my head around why {-7,3,2}!=3 is not True. $\endgroup$ – Aharon Naiman Jan 24 '19 at 15:59
3
$\begingroup$

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

| improve this answer | |
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – Aharon Naiman Jan 24 '19 at 15:57
  • $\begingroup$ 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?) $\endgroup$ – Aharon Naiman Jan 29 '19 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.