- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- Haskell Program to Convert Array to Set (HashSet)
- Haskell Program to Convert List to a Map
- C++ Program to Convert List to Set
- Golang Program to Convert List to Set
- Haskell Program to Convert Set of String to Array of String
- Program to convert Set to List in Java
- Java Program to convert a List to a Set
- Haskell Program to Convert String to Object
- Haskell Program to convert Binary to Octal
- Haskell Program to convert Decimal to Hexadecimal
- Haskell Program to convert Decimal to Binary
- Haskell Program to convert Binary to Decimal
- Haskell Program to Convert Boolean to String
- Haskell Program to Convert Data to Hexadecimal
- Haskell Program to Convert Character to String
