Search code examples
listhaskellsyntaxlist-comprehensionunique

Checking for all Elements in a Set in Haskell using syntactic sugar


I try to remove the Integer duplicates of a List of (String, Int), where I am guaranteed that there is no String duplicate.

Is it possible to evaluate something like this in Haskell:

I tried:

[(a,b) | (a,b) <- bs, (c,k) <- bs, ((k == b) <= (a == c))]

but this does not yet work.

Edit: I am well aware, that you can achieve that using more complex syntax. For example by recursively searching the List for each elements duplicates...


Solution

  • (NB: this is a completely new version of this answer. Previous was totally off-base.)

    To follow your mathematical set comprehension more closely, we can tweak the definition in your answer as

    uniquesOnly :: (Eq a, Eq b) => [(a, b)] -> [(a, b)]
    uniquesOnly bs = 
       [(a,b) | (a,b) <- bs, 
                [(c,d) | (c,d) <- bs, d == b] ==
                [(a,d) | (c,d) <- bs, d == b]]
    

    "for all (c,d) in bs such that d==b it follows c==a".

    uniquesOnly [(1,1),(2,2),(3,1)] returns [(2,2)].