Delete repeated items from a list

0

I am trying to make a function in haskell, where a list enters it by parameter and returns or shows the list but that the duplicates are eliminated, that is to say:

Enter [1,2,3,4,1,2] and show [1,2,3,4] . It can also be with characters not necessarily with numbers.

Something like this:

deleteDuplicate :: [a] -> [a]
    
asked by Jesús Henríquez 23.08.2018 в 04:16
source

1 answer

2

Haskell's Data.List module already has a function, nub , which removes repeated items from a list in% O(n^2) .

A simple way to define this function is to take the head x of the list (x:xs) , eliminate the occurrences of x in the queue xs (filtering the different elements of x using the function filter ) and recursively remove the repeated elements.

deleteDuplicate :: (Eq a) => [a] -> [a]
deleteDuplicate [] = []
deleteDuplicate (x:xs) = x : deleteDuplicate (filter (/= x) xs)
    
answered by 24.08.2018 / 13:09
source