Introduction to Haskell Map
Whenever we want to apply a function on each element of a given list and produce a new list consisting of the updated elements, then we make use of a function called map() function in Haskell and this map() function takes a list and the function to be applied on each element in the list as an input and returns a new list as the output and this map() function is available in Data. Map module and the internal implementation of map is a balanced binary tree and this is a very efficient representation in Haskell programming language when compared to the other implementations such as Hash table.
The syntax to define map in Haskell is as follows:
map :: (a -> b) -> [a] -> [b]
where (a -> b) is the function to be applied on each element in the list and
[a]->[b] represents function being applied on each element in the list.How Map works in Haskell?
Working of map in Haskell is as follows:
- Whenever we want to apply a function on each element of a given list and produce a new list consisting of the updated elements, then we make use of a function called map() function in Haskell.
- The map() function takes two parameters namely a list and the function to be applied on each element in the list and returns a new list as the output.
- The map() function is available in Data. Map module in Haskell programming language.
- The internal implementation of map is a balanced binary tree and this is a very efficient representation in Haskell programming language when compared to the other implementations such as Hash table.
Examples
Lets us discuss examples of the Haskell Map.
Example #1
Haskell program to demonstrate map function using which we are adding 2 to each element in the given list and display the resulting new list as the output on the screen:
--defining a main function in which we are using the map function on a list to add 2 to each element in the list and display the resulting new list as the output on the screen
main = do
let new = map (+2) [10, 20, 30, 40, 50]
putStrLn "The elements in the new list after using map function is:\n"
print $ new

4.8 (14,175 ratings)
View Course
The output of the above program is as shown in the snapshot below:
In the above program, we are defining a main function within which we are using the map function on a given list to add 2 to each element in the list and display the resulting list as the output on the screen.
Example #2
Haskell program to demonstrate map function using which we multiply each element in the given list by 2 and display the resulting new list as the output on the screen:
--defining a main function in which we are using the map function on a list to multiply each element in the given list by 2 and display the resulting new list as the output on the screen
main = do
let new = map (*2) [10, 20, 30, 40, 50]
putStrLn "The elements in the new list after using map function is:\n"
print $ new
The output of the above program is as shown in the snapshot below:
In the above program, we are defining a main function within which we are using the map function on a given list to multiply each element in the list by 2 and display the resulting list as the output on the screen.
Example #3
Haskell program to demonstrate map function using which we divide each element in the given list by 2 and display the resulting new list as the output on the screen:
--defining a main function in which we are using the map function on a list to divide each element in the given list by 2 and display the resulting new list as the output on the screen
main = do
let new = map (/2) [10, 20, 30, 40, 50]
putStrLn "The elements in the new list after using map function is:\n"
print $ new
The output of the above program is as shown in the snapshot below:
In the above program, we are defining a main function within which we are using the map function on a given list to divide each element in the list by 2 and display the resulting list as the output on the screen.
Example #4
Haskell program to demonstrate map function using which we subtract each element in the given list by 2 and display the resulting new list as the output on the screen:
--defining a main function in which we are using the map function on a list to subtract each element in the given list by 2 and display the resulting new list as the output on the screen
main = do
let new = map (2-) [10, 20, 30, 40, 50]
putStrLn "The elements in the new list after using map function is:\n"
print $ new
The output of the above program is as shown in the snapshot below:
In the above program, we are defining a main function within which we are using the map function on a given list to subtract each element in the list by 2 and display the resulting list as the output on the screen.
Conclusion
In this article, we have learned the concept of a map in Haskell programming language through definition, syntax, and working of map in Haskell programming language with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
This is a guide to Haskell Map. Here we discuss the Introduction, syntax, How to work Haskell Map? and examples with code implementation respectively. You may also have a look at the following articles to learn more –