Function Repository Resource:

AllSameQ

Source Notebook

Test if all arguments of an expression are identical

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["AllSameQ"][{e1,e2,}]

yields True if all the ei are identical, and yields False otherwise.

Details

The argument to ResourceFunction["AllSameQ"] need not have head List.
ResourceFunction["AllSameQ"] is usually equivalent to Apply[SameQ].
ResourceFunction["AllSameQ"] requires exact correspondence between expressions, except that it still considers Real numbers equal if they differ in their last binary digit.
ResourceFunction["AllSameQ"][{2,2.}] gives False.
ResourceFunction["AllSameQ"][{}] and ResourceFunction["AllSameQ"][{expr}] always yield True.

Examples

Basic Examples (1) 

Test whether all expressions in a list are identically the same:

In[1]:=
ResourceFunction["AllSameQ"][{x, y}]
Out[1]=
In[2]:=
ResourceFunction["AllSameQ"][{x, x, x}]
Out[2]=

Scope (4) 

Numbers in different representations are not the same:

In[3]:=
ResourceFunction["AllSameQ"][{0., 0}]
Out[3]=

The argument need not be a list:

In[4]:=
ResourceFunction["AllSameQ"][f[x, x, x]]
Out[4]=
In[5]:=
ResourceFunction["AllSameQ"][f[x, y, z]]
Out[5]=

For associations, AllSameQ operates on values:

In[6]:=
ResourceFunction["AllSameQ"][<|"a" -> 1, "b" -> 1|>]
Out[6]=
In[7]:=
ResourceFunction["AllSameQ"][<|"a" -> 1, "b" -> 2|>]
Out[7]=

Use Unevaluated to test arguments without evaluation:

In[8]:=
ResourceFunction["AllSameQ"][{Print[1], Print[2]}]
Out[8]=
In[9]:=
ResourceFunction["AllSameQ"][Unevaluated[{Print[1], Print[2]}]]
Out[9]=

Properties and Relations (3) 

In many cases, AllSameQ[expr] is effectively equivalent to Apply[SameQ,expr]:

In[10]:=
Apply[SameQ, f[x, x, x]]
Out[10]=
In[11]:=
Apply[SameQ, f[x, y, z]]
Out[11]=

An exception is that AllSameQ will not evaluate held arguments:

In[12]:=
ResourceFunction["AllSameQ"][Hold[Print[1], Print[2]]]
Out[12]=

Compare with Apply[SameQ,expr]:

In[13]:=
Apply[SameQ, Hold[Print[1], Print[2]]]
Out[13]=

Possible Issues (1) 

AllSameQ expects nonatomic expressions:

In[14]:=
ResourceFunction["AllSameQ"][123]
Out[14]=

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 23 September 2023

Related Resources

License Information