过滤元组列表

时间:2014-06-18 02:35:34

标签: haskell

我正在尝试过滤第一个元组值等于list of 2-tuples的{​​{1}}:

0

我想要的输出:

ghci> ys [(0,55),(1,100)] ghci> filter (\x -> x.fst == 0) ys <interactive>:71:27: Couldn't match type `(Integer, Integer)' with `b0 -> c0' Expected type: [b0 -> c0] Actual type: [(Integer, Integer)] In the second argument of `filter', namely `ys' In the expression: filter (\ x -> x . fst == 0) ys In an equation for `it': it = filter (\ x -> x . fst == 0) ys

我怎样才能做到这一点?另外,编译时错误是什么意思?

1 个答案:

答案 0 :(得分:8)

(.)是函数组合,你想要 filter (\x -> fst x == 0) ys 。 修改:您实际上需要filter (\x -> fst x /= 0) ys,因为filter提供满足谓词的值列表。

编译时错误令人抱怨,因为编译器推断x必须是函数,因为您使用fst编写它,但ys不是函数列表。< / p>

相关问题