Domanda

I'm trying to filter a list of tuples in haskell. I want to return the tuples where the first and the second element are the same.

I'm trying this one

 filter ((==fst).snd) [(1,2), (2,2), (3,3)] 

but it doesn't work. Any ideas on this? Thank you!

È stato utile?

Soluzione

You can use uncurry:

filter (uncurry (==)) [(1,2), (2,2), (3,3)]

alternatively you can match on each tuple:

filter (\(x,y) -> x == y) [(1,2), (2,2), (3,3)]

Altri suggerimenti

Try this:

filter (\p -> fst p == snd p) [(1,2), (2,2), (3,3)]

(==fst).snd means \p -> snd p == fst, obviously this would not work. If you really do not want to use lambda abstraction, and want a pointless version, here is one way to achieve that (you need to import Control.Applicative first):

filter ((==) <$> fst <*> snd) [(1,2), (2,2), (3,3)]
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top