Function Repository Resource:

SameAsQ

Source Notebook

An operator form of SameQ

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["SameAsQ"][this][that]

yields True if the expression that is literally this, and yields False otherwise.

ResourceFunction["SameAsQ"][x1,x2,][y1,y2,]

is equivalent to SameQ[x1,x2,,y1,y2,].

Details and Options

ResourceFunction["SameAsQ"] requires exact correspondence between expressions, except that it still considers Real numbers equal if they differ in their last binary digit.
ResourceFunction["SameAsQ"][2][2.] gives False.
ResourceFunction["SameAsQ"][] is equivalent to SameQ.
ResourceFunction["SameAsQ"][][] is equivalent to SameQ[].

Examples

Basic Examples (3) 

Test whether two expressions are identically the same:

In[1]:=
ResourceFunction["SameAsQ"][x][y]
Out[1]=
In[2]:=
ResourceFunction["SameAsQ"][x][x]
Out[2]=

Separate a specific element from a list:

In[3]:=
GroupBy[{a, y, 1, 2, a, 3, x, a, x, 2}, ResourceFunction["SameAsQ"][a]]
Out[3]=

Test equivalence of strings:

In[4]:=
ResourceFunction["SameAsQ"]["this"]["THIS"]
Out[4]=

Scope (3) 

Numbers in different representations are not the same:

In[5]:=
ResourceFunction["SameAsQ"][0.][0]
Out[5]=

== nevertheless treats them as equal:

In[6]:=
0. == 0
Out[6]=

Approximate numbers that differ in their last bit are still considered identical:

In[7]:=
ResourceFunction["SameAsQ"][
  2^^1.000000000000000000000000000000000000000000000000000000000000][2^^1.000000000000000000000000000000000000000000000000000000000001]
Out[7]=

Compare unevaluated expressions:

In[8]:=
test = ResourceFunction["SameAsQ"][Unevaluated[Echo[1 + 1]]]
Out[8]=
In[9]:=
test[Echo[1 + 1]]
Out[9]=
In[10]:=
test[Unevaluated[Echo[1 + 1]]]
Out[10]=

Without the Unevaluated, expressions will be compared with 2:

In[11]:=
test = ResourceFunction["SameAsQ"][Echo[1 + 1]]
Out[11]=
In[12]:=
test[Echo[1 + 1]]
Out[12]=
In[13]:=
test[Unevaluated[Echo[1 + 1]]]
Out[13]=

Properties and Relations (4) 

SameQ cannot have an operator form, since a single element is the same as itself:

In[14]:=
SameQ[x]
Out[14]=

SameAsQ can be used as an operator form for SameQ:

In[15]:=
xQ = ResourceFunction["SameAsQ"][x]
Out[15]=
In[16]:=
xQ[x]
Out[16]=
In[17]:=
xQ[y]
Out[17]=

Multiple arguments can be given in either position:

In[18]:=
ResourceFunction["SameAsQ"][a, a, a][a, a]
Out[18]=
In[19]:=
ResourceFunction["SameAsQ"][a, a, a][b, a]
Out[19]=

This function will always return False:

In[20]:=
neverTrue = ResourceFunction["SameAsQ"][1, 2]
Out[20]=
In[21]:=
neverTrue[1, 2]
Out[21]=
In[22]:=
neverTrue[]
Out[22]=

This is effectively equivalent to the following:

In[23]:=
neverTrue2 = SameQ[1, 2, ##] &
Out[23]=
In[24]:=
neverTrue2[1, 2]
Out[24]=
In[25]:=
neverTrue2[]
Out[25]=

Absolutely nothing is the same as absolutely nothing:

In[26]:=
ResourceFunction["SameAsQ"][][]
Out[26]=

This is equivalent:

In[27]:=
SameQ[]
Out[27]=

Applications (2) 

Create FalseQ, a variant of TrueQ that does what you probably expect:

In[28]:=
FalseQ = ResourceFunction["SameAsQ"][False]
Out[28]=
In[29]:=
FalseQ[a == b]
Out[29]=
In[30]:=
TrueQ[a == b]
Out[30]=
In[31]:=
FalseQ[a === b]
Out[31]=
In[32]:=
TrueQ[a === b]
Out[32]=
In[33]:=
FalseQ[a === a]
Out[33]=
In[34]:=
TrueQ[a === a]
Out[34]=

Compare:

In[35]:=
TableForm[Table[f[x], {f, {TrueQ, FalseQ}}, {x, {True, False, maybe}}],
 TableHeadings -> {{"TrueQ", "FalseQ"}, {True, False, maybe}}]
Out[35]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 20 March 2019

Related Resources

License Information