0

我的列表是 [[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)

在这种情况下,没有考虑元素的顺序,这意味着[“A1”,“A2”,“A3”]和[“A2”,“A3”,“A1”]是重复的。

我想使用“nubBy”和“compareTo”函数来构建我的 removeDuplication 函数,但我不确定如何将一个元素与列表中的所有其他元素进行比较。

4

2 回答 2

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"]
于 2013-09-18T09:05:36.913 回答
0

您已经完成了解决问题的所有点点滴滴。

您只需要编写最后一行:

removeDuplication = nubBy compareTo

修复剩余的错别字后,一切都应该可以正常工作。

于 2013-09-18T06:09:04.210 回答