考虑名单从列表中过滤掉空列表
[[],[1],[1,2],[1,2,3],[],[2],[2,3],[],[3],[]]
我想筛选出不属于空列表,即滤波输出应该给我像一个结果的所有元素:
[[1],[1,2],[1,2,3],[2],[2,3],[3]]
下面的代码失败:
myfilter lst = filter(\x -> x/=[]) lst
与以下错误为[12,3,[]]
No instance for (Num [a])
arising from the literal `3' at <interactive>:1:13
Possible fix: add an instance declaration for (Num [a])
In the expression: 3
In the first argument of `myfilter', namely `[12, 3, []]'
In the expression: myfilter [12, 3, []]
它为什么失败?您提供的代码提供了与所需输出完全匹配的结果。也许你需要详细说明究竟出了什么问题。 – fuz
这不会解决你的问题,但作为一种风格的东西,它通常被认为是使用'null'或'not'的好形式。 null“而不是'(== [])'或'(/ = [])',因为它不需要虚假的'Eq'实例。 –
是的,这会更优雅:'myFilter = filter(not.null)'。 – Landei