- 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 a Map
In Haskell, we will convert List to a Map by using fromList and foldl functions. This can also be done using recursion and pattern matching. In the first example, we are going to use (listToMap xs = Map.fromList xs) function and in the second example, we are going to use (listToMap xs = List.foldl' (\acc (k, v) -> Map.insert k v acc) Map.empty xs) function. In the third example, we are going to use base and recursive case of the recursive function.
Algorithm
Step 1 − The qualified Data.Map module is imported.
Step 2 − The listToMap function is defined using fromList function
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, ‘myList’ is defined that will hold the List Value that is to be converted to map.
Step 5 − The resultant map 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 Map using fromList function.
import qualified Data.Map as Map listToMap :: Ord k => [(k, v)] -> Map.Map k v listToMap xs = Map.fromList xs main :: IO () main = do let myList = [("apple", 1), ("banana", 2), ("orange", 3)] let myMap = listToMap myList print myMap
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... fromList [("apple",1),("banana",2),("orange",3)]
Example 2
In this example, List is converted to Map using foldl function.
import qualified Data.List as List import qualified Data.Map as Map listToMap :: Ord k => [(k, v)] -> Map.Map k v listToMap xs = List.foldl' (\acc (k, v) -> Map.insert k v acc) Map.empty xs main :: IO () main = do let myList = [("apple", 1), ("banana", 2), ("orange", 3)] let myMap = listToMap myList print myMap
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... fromList [("apple",1),("banana",2),("orange",3)]
Example 3
In this example, List is converted to Map using recursion and pattern matching.
import qualified Data.Map as Map listToMap :: Ord k => [(k, v)] -> Map.Map k v listToMap [] = Map.empty listToMap ((k, v):xs) = Map.insert k v (listToMap xs) main :: IO () main = do let myList = [("apple", 1), ("banana", 2), ("orange", 3)] let myMap = listToMap myList print myMap
Output
[1 of 1] Compiling Main ( main.hs, main.o ) Linking main ... fromList [("apple",1),("banana",2),("orange",3)]
Conclusion
In Haskell, a list can be converted to a Map data structure where each element in the list is considered as a key-value pair. The Map data structure is defined in the Data.Map module and is a container that stores a set of key-value pairs. To convert a list to a Map, we can use the fromList function provided by the Data.Map module. The fromList function takes a list of key-value pairs and returns a Map that maps each key to its corresponding value. We can also use foldl function and recursion along with pattern matching for this conversion.
- Related Articles
- Golang Program to Convert List to Map
- Haskell Program to Convert List to Set
- Java Program to convert Properties list into a Map
- Java program to convert the contents of a Map to list
- Java Program to convert a Map to a read only map
- How to convert a List to a Map in Kotlin?
- Program to convert a Map to a Stream in Java
- 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
