开发者

Filtering list of tuples

开发者 https://www.devze.com 2022-12-09 14:24 出处:网络
New to Haskell and have a stumbling block. I\'m trying to filter a list of tuples based on the first item.

New to Haskell and have a stumbling block. I'm trying to filter a list of tuples based on the first item.

filter (==(x,_)) lis

I get an illegal '_' error, but I'm not sure how I c开发者_运维技巧an get around it?


In Haskell, you cannot iterate over a tuple like you can a list.

If the tuple only has two items, you can use fst to retrieve the first item of the tuple and snd to retrieve the second item.

One way to do what I think you want to do is this approach:

Prelude> let lst = [(1,2), (3,4)]
Prelude> filter ((==1).fst) lst
[(1,2)]

Which only returns the items in the list where the first element is equal to 1; of course, you can substitute x where I put 1.

To be a little more specific, (==1).fst first applies fst to the element in lst, then applies (==1) to the result of fst -- technically, the dot composes the two functions together.


You can't give an argument with a wildcard _ in it to the == operator (or to any other function). The argument needs to be a real value, not a pattern that should be matched against.

If you want to use pattern matching you could use a lambda function as you filter condition:

filter (\(a,_) -> a == x) lis

Also, there is the predefined function fst to extract the first element of a two-element tuple. This can be combined with == to do the same test:

filter ((== x) . fst)) lis
0

精彩评论

暂无评论...
验证码 换一张
取 消