Haskell Program to Convert List to Set


Haskell programming has internal functions like fromList, Set.fromList and foldr functions that can be used to convert list to set. In the first example, we are going to use (listToSet = Set.fromList) function and in the second example, we are going to call (set = Set.fromList xs) function directly from the main function. Whereas in third example, we are going to use (listToSet = foldr Set.insert Set.empty) function.

Algorithm

  • Step 1 − The qualified Data.Set module is imported.

  • Step 2 − The listToSet function is defined using fromList function as,

    listToSet = Set.fromList.

  • Step 3 − The program execution will be started from main function. The main() function has whole control of the program. It is written as main = do.

  • Step 4 − The variable, ‘xs’ is defined that will hold the List Value that is to be converted to set.

  • Step 5 − The resultant set value corresponding to the list value is printed to the console, once the function is being called.

Example 1

In this example, List is converted to Set using fromList function.

import qualified Data.Set as Set

listToSet :: Ord a => [a] -> Set.Set a
listToSet = Set.fromList

main :: IO ()
main = do
  let xs = [1, 2, 3, 2, 1, 4]
      set = listToSet xs
  putStrLn $ "List: " ++ show xs
  putStrLn $ "Set: " ++ show (Set.toList set)

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
List: [1,2,3,2,1,4]
Set: [1,2,3,4]

Example 2

In this example, List is converted to Set using fromList function under main function.

import qualified Data.Set as Set

main :: IO ()
main = do
  let xs = [1, 2, 3, 2, 1, 4]
      set = Set.fromList xs
  putStrLn $ "List: " ++ show xs
  putStrLn $ "Set: " ++ show (Set.toList set)

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
List: [1,2,3,2,1,4]
Set: [1,2,3,4]

Example 3

In this example, List is converted to Set using fromList function using user-defined function.

import qualified Data.Set as Set

listToSet :: Ord a => [a] -> Set.Set a
listToSet xs = Set.fromList [x | x <- xs]

main :: IO ()
main = do
  let xs = [1, 2, 3, 2, 1, 4]
      set = listToSet xs
  putStrLn $ "List: " ++ show xs
  putStrLn $ "Set: " ++ show (Set.toList set)

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
List: [1,2,3,2,1,4]
Set: [1,2,3,4]

Example 4

In this example, List is converted to Set using foldr function.

import qualified Data.Set as Set

listToSet :: Ord a => [a] -> Set.Set a
listToSet = foldr Set.insert Set.empty

main :: IO ()
main = do
  let xs = [1, 2, 3, 2, 1, 4]
      set = listToSet xs
  putStrLn $ "List: " ++ show xs
  putStrLn $ "Set: " ++ show (Set.toList set)

Output

[1 of 1] Compiling Main             ( main.hs, main.o )
Linking main ...
List: [1,2,3,2,1,4]
Set: [1,2,3,4]

Conclusion

In Haskell, a list is an ordered collection of elements of the same type, while a set is an unordered collection of distinct elements of the same type. The process of converting a list to a set involves removing any duplicates and ordering the remaining elements. Haskell provides the Data.Set module, which defines the Set type and several functions for working with sets. To convert a list to a set, we typically use the fromList function from the Data.Set module. We can also use foldr function for this conversion.

Updated on: 28-Mar-2023

328 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements