Updated April 11, 2023
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 does Map work 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 some of the examples.
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
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
,/pre>
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 the definition, syntax, and working with corresponding programming examples and their outputs to demonstrate them.
Recommended Articles
We hope that this EDUCBA information on “Haskell Map” was beneficial to you. You can view EDUCBA’s recommended articles for more information.