所以我试图从2个集合列表中创建一个setSubtraction,它返回xs中但不是ys中出现的所有元素的集合。例如xs = [1,2,3]和ys = [3,4,5],则setSubtraction xs ys = [1,2]
这是我到目前为止所拥有的
setSubtract xs ys = [x | x <- xs, y <- ys, filter (==y) xs]
但过滤功能似乎错误
答案 0 :(得分:2)
我稍微更改了您的代码,并过滤(== x)
ys == []
检查x
未显示ys
。
这是定义:
setSubtract xs ys = [x | x <- xs, filter ( == x) ys == []]
这是在行动:
λ> setSubtract [1,2,3] [3,4,5]
[1,2]
λ> setSubtract [1,2,3,4,5,6,7] [3,4,5]
[1,2,6,7]
λ>
替代定义
setSubtract xs ys = [x | x <- xs, not . any ( == x) $ ys]
答案 1 :(得分:0)
setSubtract cmp xs ys = [x | x <- xs, member cmp x ys == False]