Eliminating the duplicates completely in Haskell
haskell, list
Solution
The code is almost correct. Just change this line
| isTheSame xs x = eliminate xs
to
| isTheSame xs x = eliminate $ filter (/=x) xs
The reason is that if `x` is contained in `xs`, you want to delete all occurences of `x`.
That said, there are a few parts in your code sample that could be expressed more elegantly:
- `(fst x) == (fst a) && (snd x) == (snd a)` is the same as `x == a`
- `isTheSame` is the same as `elem`, only with its arguments reversed
Thus, we could express the function `eliminate` like this:
eliminate [] = []
eliminate (x:xs)
| x `elem` xs = eliminate $ filter (/=x) xs
| otherwise = x : eliminate xs
Problem
I have this code but it does not do what I want totally, I takes a list of tuples; ``` [(3,2),(1,2),(1,3),(1,2),(4,3),(3,2),(1,2)] ``` and gives ``` [(1,3),(4,3),(3,2),(1,2)] ``` but I want it to give ``` [(1,3),(4,3)] ``` where am I doing wrong? Thanks in advance. ``` eliminate :: [(Int,Int)] -> [(Int,Int)] eliminate [] = [] eliminate (x:xs) | isTheSame xs x = eliminate xs | otherwise = x : eliminate xs isTheSame :: [(Int,Int)] -> (Int,Int) -> Bool isTheSame [] _ = False isTheSame (x:xs) a | (fst x) == (fst a) && (snd x) == (snd a) = True | otherwise = isTheSame xs a ```