Why are the following functions

a[x___] := If[ UnsameQ[x, Null],    1, 2, 3]
b[x___] := If[ Not[SameQ[x, Null]], 1, 2, 3]

different? For example


In particular, why does a[] gives 1 instead of 2?

I didn't expect that also because

UnsameQ[x, y] == Not[SameQ[x, y]]
UnsameQ[x, Null] == Not[SameQ[x, Null]]

Moreover, if I define

uns[a_, b_] := UnsameQ[a, b]
nts[a_, b_] := Not[SameQ[a, b]]

the functions

c[x___] := If[nts[x, Null], 1, 2, 3]
d[x___] := If[uns[x, Null], 1, 2, 3]

are different from the previous ones:


Why does it happens?

The same question holds for SameQ and Not[UnsameQ]

  • 1
    $\begingroup$ You're actually evaluating UnsameQ[Null] and SameQ[Null] both of which return True. The BlankNullSequence spits out something akin to Sequence[] if you have no match. $\endgroup$ – b3m2a1 May 31 '17 at 4:52
  • 4
    $\begingroup$ One is the negation of the other with two arguments only. They take any number of arguments. When used with fewer or more arguments, the identity does not hold. Another example would be SameQ[a, b, a] and UnsameQ[a, b, a], which are both false, as these expressions are neither all identical, nor all different. $\endgroup$ – Szabolcs May 31 '17 at 7:25
  • 1
    $\begingroup$ The behaviour of SameQ is weird if there is only one parameter. SameQ[x] == True. According to the documentation, "lhs===rhs yields True if the expression lhs is identical to rhs, and yields False otherwise. " If there is no rhs, SameQ should return False, however it doesn't. $\endgroup$ – UnchartedWorks May 31 '17 at 8:01
  • $\begingroup$ This is far from trivial. Voting "leave open". $\endgroup$ – LLlAMnYP Jun 2 '17 at 11:09
  • $\begingroup$ @UnchartedWorks from the docs, e1===e2===e3 gives True if all the ei are identical. (equivalent to SameQ[e1,e2,e3]. Of course in a set of just [e1] all of the ei are identical. No reason to return False. $\endgroup$ – LLlAMnYP Jun 2 '17 at 11:15

You seem to assume that a[] and b[] are the same as a[Null] and b[Null], but they are not. If you were to run a Trace on a[] and b[], you would see


{a[], If[UnsameQ[Null], 1, 2, 3], {UnsameQ[Null], True}, If[True, 1, 2, 3], 1}


{b[], If[!SameQ[Null], 1, 2, 3], {{SameQ[Null], True}, !True, False}, If[False, 1, 2, 3], 2}

Both UnsameQ and SameQ are only seeing one argument, which is an edge case not discussed in the Mathematica documentation (), but the traces show how of these functions behave differently when given a single argument, explaining the differences you are seeing.

  • $\begingroup$ An illustration: f[x___] := HoldComplete@x; f[] returns HoldComplete[]. $\endgroup$ – Alexey Popkov May 31 '17 at 5:16
  • $\begingroup$ That's is interesting: so let's say that if I have a function f[a_,b___] and I want that if I input only one argument I get f[a]=a but if I input 2 arguments I get f[a,b]=2a+b. How can I implement that? $\endgroup$ – Giancarlo May 31 '17 at 6:47
  • $\begingroup$ @Giancarlo. Please don't ask a new question in a comment. However, in the case you bring up in your comment, I would suggest f[x_] := x; f[x_, y_] := 2 x + y $\endgroup$ – m_goldberg May 31 '17 at 7:01
  • $\begingroup$ Thanks m_goldberg. Related to the original question, it's still not clear to me why a[] is different from d[], and, similarly, b[] is different from c[]. Do you have any idea why? $\endgroup$ – Giancarlo May 31 '17 at 8:58

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