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.

Updated on: 28-Mar-2023

334 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements