目标:查找列表的排列,例如[“abc”,“bc”,“acc”]
问题:我的排列包含加倍和三倍的元素,我想摆脱这些元素。
我从上面的列表中获得的结果是:
[[ “ABC”, “BC”, “ACC”],[ “ABC”, “BC”],[ “ABC”, “ACC”, “BC”],[ “ABC”, “ACC” ],[ “ABC”, “ACC”],[ “ABC”],[ “BC”, “ABC”, “ACC”],[ “BC”, “ABC”],[ “BC”, “ACC” , “ABC”],[ “BC”, “ACC”],[ “BC”, “ACC”],[ “BC”],[ “ACC”, “ABC”, “BC”],[ “ACC” , “ABC”],[ “ACC”, “BC”, “ABC”],[ “ACC”, “BC”],的 [ “ACC”, “BC”] 下,[“ACC “],[” BC”, “ACC”],[ “BC”],的 [ “ACC”, “BC”] 下,[ “ACC”],[ “ACC”],[] ]
我为了摆脱那些加倍的元素而编写的代码如下所述:
fct [] = []
fct (xs)
| (head xs) `elem` xs = fct (delete (head xs) xs)
| otherwise = fct xs
在这里,我想获取列表的第一个元素,并将其与列表的其余部分进行比较。 你能帮我找到解决问题的方法吗?
答案 0 :(得分:2)
这些不是排列,因为其中一些不包含所有原始元素。它似乎是排列和选择的组合。
在ghci:
Prelude Data.List Control.Monad> concatMap permutations $ filterM (const [False, True])["abc", "bc", "acc"]
[[],["acc"],["bc"],["bc","acc"],["acc","bc"],["abc"],["abc","acc"],["acc","abc"], ["abc","bc"],["bc","abc"],["abc","bc","acc"],["bc","abc","acc"],["acc","bc","abc"],["bc","acc","abc"],["acc","abc","bc"],["abc","acc","bc"]]
答案 1 :(得分:0)
您可以使用功能nub删除列表中的重复项。