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]
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]
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)