How to find duplicates in the list of tuples?

What would be the best way to filter through a list of tuples and return only those where the values fstand sndmatch?

[(2,1),(2,2),(3,1),(10,9),(10,10)] 

will return (2,2)and (10,10).

+4
source share
2 answers

The easiest way - just use filtera lambda: filter (\ (a, b) -> a == b) ls.

You can also be pretty and use uncurrythat changes the normal function of two arguments to one that takes a tuple, providing you filter (uncurry (==)). Remember that (==)it is just a type function Eq a => a -> a -> Bool, therefore it uncurry (==)is a type function Eq a => (a, a) -> Bool, which is exactly what you are looking for.

+12

, :

doubles ls = [(x,y) | (x,y) <- ls, x==y]
+4

Source: https://habr.com/ru/post/1568225/


All Articles