How to remove duplicates from a list in Haskell
Haskell is a purely functional programming language with strong typing and lazy evaluation. When it comes to removing duplicates from a list, multiple approaches are available. One option is to utilize the Data.Set module for removing duplicates. Alternatively, you can create your own custom functions that are specifically designed to remove duplicates.
We will look at two different methods: using the Data.Set module and creating a custom function with the help of filter
Removing duplicates with Data.Set
We can use the Data.Set module to remove duplicates from a list. This module offers a built-in function that converts the list into a set. Duplicates are automatically removed during this conversion because sets only store distinct elements. Then we convert the set back into a list, effectively eliminating all duplicates present in the list.
import Data.Set (fromList, toList)removeDuplicates :: (Eq a, Ord a) => [a] -> [a]removeDuplicates = toList . fromListmain :: IO ()main = dolet inputList = [1, 2, 3, 4, 3, 2, 1]outputList = removeDuplicates inputListputStrLn "Input List:"print inputListputStrLn "Output List (after removing duplicates):"print outputList
Line 1: The code imports the necessary functions
fromListandtoListfrom theData.Setmodule in Haskell.Line 3: The
removeDuplicatesfunction is defined with a type constraint(Eq a, Ord a) => [a] -> [a], indicating that it works for any typeathat is both (equatable In Haskell, "equatable" means that values of a type can be compared for equality using the equality operator (==). Eq a) and (orderable In Haskell, "orderable" means that values of a type can be compared and ordered using comparison operators such as <, >, <=, and >=. Ord a).Line 4: The function removes duplicates from a given list by converting it to a set using
fromListand then converting it back to a list usingtoList.Lines 6–13: In the
mainfunction, an example input list[1, 2, 3, 4, 3, 2, 1]is defined. The program then prints the original input list and the resulting output list after removing duplicates.
The algorithm's time complexity is
Removing duplicates using filter
We can use filter function which is a built-in function in Haskell that doesn't require any module imports. It allows us to filter elements from a list selectively based on a specified condition.
We will use the filter as a helper function which will operate on the parent function, which will be called recursively, continuing to remove duplicates until all duplicates are eliminated or an empty list is encountered.
Code
The following code snippet demonstrates the implementation of removing duplicates using Haskell. It includes a main function and a parent function called removeDuplicates, which uses the filter function to eliminate duplicate elements.
removeDuplicates :: Eq a => [a] -> [a]removeDuplicates [] = []removeDuplicates (x:xs) = x : removeDuplicates (filter (/= x) xs)main :: IO ()main = dolet inputList = [1, 2, 3, 4, 3, 2, 1]outputList = removeDuplicates inputListputStrLn "Input List:"print inputListputStrLn "Output List (after removing duplicates):"print outputList
Line 1: The
removeDuplicatesfunction takes a list[a]as input and returns a list[a]as output. TheEq aconstraint specifies that the elements of the list must be comparable for equality.Line 2: The first line states the base case: if the input list is empty (
[]), the result is also an empty list ([]). This serves as the termination condition for the recursion.Line 3: It matches the input list
(x:xs)into its head elementxand the remaining tailxs. The result is constructed by appendingxto the output of recursively callingremoveDuplicateson the filtered tailxs.The
filterfunction is used to remove elements fromxsthat are equal tox. The expression(/= x)is a function that checks if an element is not equal tox. Sofilter (/= x) xsremoves all occurrences ofxfromxs.
Lines 5–12: The
mainfunction is responsible for creating an input list, applying theremoveDuplicatesfunction to it, and printing both the input and output lists usingputStrLnandprintfunctions.
The algorithm has nested recursive calls, with each call potentially traversing the remaining list elements using the filter function. This results in a time complexity of
Free Resources