user2787443
user2787443

Reputation: 15

Remove duplication elements in the list Haskell

My list is a [[String]] and it looks like [["A1","A2","A3"],["A1","A2","B1"],["A1","A2","B2"].....]

my codes is shown below

List = [[x,y,z] | x <- l1, y<- l2, z <- l3]
            where l1 = ["A1","A2","A3","B1","B2","B3","C1","C2","C3","D1","D2","D3"];
                          l2 = ....;
                          l3 = ....

compareTo :: [String] -> [String] -> Bool
compareTo x y
            |length (intersect x y) == length x     =True
            |otherwise                              =False 

removeDuplication :: [[String]] -> [[String]]
removeDuplication (x:xs) = nubBy compareTo (x:xs)

In this case, the order of elements has not taken into consideration, which measn ["A1", "A2", "A3"] and ["A2", "A3", "A1"] are duplications.

I want to use 'nubBy' and 'compareTo' functions to build up my removeDuplication function, and I'm not sure how to compare an element to all other elements in the list.

Upvotes: 0

Views: 185

Answers (2)

Sassa NF
Sassa NF

Reputation: 5406

It looks like you are trying to generate lists that are permutations of all Strings in l1. So maybe you don't need nub and compareTo.

list = [[x,y,z] | (x:xs) <- tails l1, (y:ys) <- tails xs, z <- ys]
  where l1 = ["A1","A2","A3","B1","B2","B3","C1","C2","C3","D1","D2","D3"]

Upvotes: 1

michas
michas

Reputation: 26555

You already finished all the bits and pieces to solve your problem.

You just have to compose the final line:

removeDuplication = nubBy compareTo

After fixing the remaining typos everything should work fine.

Upvotes: 0

Related Questions