让我们看看代码,看看沿途发生了什么。
我们定义了 2 个集合
Set1 = sets:from_list("orchestra").
{set,8,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
{{[],"sc",[],[],[],"o","r","e","h",[],[],"a","t",[],[],[]}}}
和
Set2 = sets:from_list("carthorse").
{set,8,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
{{[],"sc",[],[],[],"o","r","e","h",[],[],"a","t",[],[],[]}}}
其中第二行表示使用表示创建的值。所以根据上面的定义,我们推断存储集合所选择的表示是一个元组。
我们可以参考 Erlang 参考手册的 Term Comparison 部分,其中指出
...Lists are compared element by element. Tuples are ordered by size, two tuples with the same size are compared element by element...
将其作为不变量,现在让我们看看在intersection 之后出现的集合
Intersection = sets:intersection(Set1, Set2).
{set,8,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
{{[],"cs",[],[],[],"o","r","e","h",[],[],"a","t",[],[],[]}}}
同样,这是一个集合,它得到元组的相同底层表示,但如果我们查看最后一个元素,有一个字符串(本质上是 Erlang 中的字符列表)"cs",它的值在当我们定义Set1 和Set2 时在同一个地方。因此根据Term Comparison 的不等式。
现在,让我们尝试将Intersection 中的子值"cs" 更改为"sc",然后根据Term Comparison 规则,两个集合必须满足相等。
Improvised_Intersection = setelement(9, Intersection, {setelement(2, element(1, element(9, Intersection)), "sc")}).
{set,8,16,16,8,80,48,
{[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]},
{{[],"sc",[],[],[],"o","r","e","h",[],[],"a","t",[],[],[]}}}
现在让我们将Improvised_Intersection 与Set1 和Set2 进行比较,得到
Improvised_Intersection =:= Set1.
true
Improvised_Intersection =:= Set2.
true
Intersection =:= Improvised_Intersection.
false
这只是试图对@Richard 和其他人已经指出的内容提供一些见解。