从集合中删除子集


这可能是

微不足道的,但我坚持编写一个函数,从集合本身中删除集合的子集(找到它的补码(。
我的函数形式为:

removeSubset :: (Eq a) => [a] -> [a] -> [a] 
removeSet [] ys = Just ys
removeSet --This is where I don't know how to remove the subset

任何帮助将不胜感激,因为我是哈斯克尔的新手。

你不需要在 Maybe 中包装结果,因为你可能总是返回空列表。

最简单的实现是:

removeSet xs ys = filter (not . (`elem` xs)) ys 

预计到达减少后:

removeSet xs = filter (not.(`elem`xs))

对于更多的代码高尔夫无点(无点(风格,它也可以写成:

removeSet = filter.((not.).(flip elem))

对于使用递归的更直接的解决方案,您可以始终使用:

removeSet _  []    = []
removeSet [] ys    = ys
removeSet xs (y:ys)= if element y xs then removeSet xs ys else y:removeSet xs ys
  where element x [] = False
        element x (l:ls) = if l == x then True else element x ls

最新更新