how can I delete repeated items from a list?

0

If I have this list

[a,b,c,d] y esta otra [a,b,c,a,c,b,d,a,d,c]

How can I delete the repeated elements from this list [a,b,c,a,c,b,d,a,d,c] so that I stay like this [a,b,c,d]

    
asked by Efrainrodc 26.06.2017 в 17:14
source

1 answer

1
deleteN :: Int -> [a] -> [a]
deleteN _ []     = []
deleteN n (h:t)
  | n == 0    = t
  | otherwise = h : deleteN (n-1) t

This solution is quite optimized, it is terminal recursive and only goes through the list once.

To eliminate the duplicate elements (According to the edition of the question) you can use the function nub , of the module Data.List , in this way nub lista . To not use libraries (Indicated in comment), you can consult the source:

nub                     :: (Eq a) => [a] -> [a]
nub l                   = nub' l []
  where
    nub' [] _           = []
    nub' (x:xs) ls
        | x 'elem' ls   = nub' xs ls
        | otherwise     = x : nub' xs (x:ls)
    
answered by 26.06.2017 / 18:01
source