我的列表是[[String]],它看起来像[[“A1”,“A2”,“A3”],[“A1”,“A2”,“B1”],[“A1”, “A2”, “B2”] .....]
我的代码如下所示
List = [[x,y,z] | x <- l1, y<- l2, z <- l3]
where l1 = ["A1","A2","A3","B1","B2","B3","C1","C2","C3","D1","D2","D3"];
l2 = ....;
l3 = ....
compareTo :: [String] -> [String] -> Bool
compareTo x y
|length (intersect x y) == length x =True
|otherwise =False
removeDuplication :: [[String]] -> [[String]]
removeDuplication (x:xs) = nubBy compareTo (x:xs)
在这种情况下,没有考虑元素的顺序,其中measn [“A1”,“A2”,“A3”]和[“A2”,“A3”,“A1”]是重复。< / p>
我想使用'nubBy'和'compareTo'函数来构建我的removeDuplication函数,我不知道如何将元素与列表中的所有其他元素进行比较。
答案 0 :(得分:1)
看起来您正在尝试生成列表,这些列表是l1中所有字符串的排列。所以也许你不需要nub和compareTo。
list = [[x,y,z] | (x:xs) <- tails l1, (y:ys) <- tails xs, z <- ys]
where l1 = ["A1","A2","A3","B1","B2","B3","C1","C2","C3","D1","D2","D3"]
答案 1 :(得分:0)
你已经完成了所有的部分工作来解决你的问题。
你只需要撰写最后一行:
removeDuplication = nubBy compareTo
在修复剩余的拼写错误后,一切都应该正常。