您的描述与您的第二个示例不太吻合。您说您想用 first 重复值替换该值,但在第二个示例中,您将 (3,"l") 替换为 last 重复值((3,"f") 而不是 (3,"x") )。两者都可以完成,尽管用最后一个副本替换要容易得多。
要替换为最后一个副本,请查看通过更新明确的键值对列表获得的最终列表。编写一个执行此更新的函数,然后在列表上运行此更新函数,从空列表开始:
fun update (i,c) [] = [(i,c)]
| update (i,c) ((j,d)::records) =
if i = j then
(i,c)::records
else
(j,d) :: (update (i,c) records)
fun updateAll [] records = records
| updateAll ((i,c)::pairs) records = updateAll pairs (update (i,c) records)
fun removeVarDuplicates pairs = updateAll pairs [];
此函数对您的两个示例按预期工作。
为了完整起见,这是一种方法,其中 first 重复值最终被保留。为此,请添加一个布尔标志,该标志将告诉您该值是否已更新。第一次更新 - 设置标志。去掉最终结果中的标志:
fun update (i,c) [] = [(i,c,false)]
| update (i,c) ((j,d,t)::triples) =
if i = j then
if t then (j,d,t) :: triples else (j,c,true)::triples
else
(j,d,t) :: (update (i,c) triples)
fun updateAll [] triples = triples
| updateAll ((i,c)::pairs) triples = updateAll pairs (update (i,c) triples)
fun removeVarDuplicates pairs =
let
val triples = updateAll pairs []
in
map (fn (x,y,_) => (x,y)) triples
end;
当这是针对您的第二个示例运行时:
- val mylist = [(1,"h"),(3,"l"),(45,"j"),(3, "x"), (3, "f")];
val mylist = [(1,"h"),(3,"l"),(45,"j"),(3,"x"),(3,"f")] : (int * string) list
- removeVarDuplicates mylist;
val it = [(1,"h"),(3,"x"),(45,"j")] : (int * string) list
保留第一个重复键"x" 的第一个值,而不是第二个重复键的值。
对于任何涉及键和值的严肃工作,您应该考虑使用不同的数据结构,例如 SML/NJ 的 hash table 。我上面给出的代码效率非常低,最终结果是一个带有O(n) 查找的数据结构。