Updated April 19, 2023
Introduction to Haskell list comprehension
In Haskell, we have comprehension notation which is mainly used to create the new list from the existing one. This comprehension notation works in the same way as it works on mathematics where we have a new set of values that is being produced by the existing one, in the existing one we can modify the values to produce the new list. In Haskell also we can use the same operator which is begin used in mathematics, it is represented by the pipe symbol in Haskell. To use the comprehension in Haskell we should follow the syntax given by the Haskell official documentation, which is easy to read and understand by the developers. n the comings section of the tutorial we will see the internal working of comprehension notation in Haskell, and how we can use it while programming in Haskell in detail. Also, we will see its usage and implementation.
Syntax
As we know that comprehension is annotation in Haskell which is used to produce the new list from the existing one. Now let’s take a closer look at the syntax of the comprehension notation in Haskell for beginners to understand it better see below;
[any_function variable_name | to bind <- your list]
As you can see in the above line of syntax we have just used to ‘|’ pipe operator to prepare a comprehension list. Its syntax is a bit tricky to understand but in the coming section, we will see one example to understand it better. Let’s take a closer look at the practice syntax for better clarity to see below;
Example:
[toUpper x | x <- ["hello world"]
As from the above practice syntax, it is pretty much clear to understand it now. In the coming section of the tutorial, we will see the internal working and what things are required to use this while programming in Haskell for beginners to understand and start using it efficiently.
How does list comprehension work in Haskell?
As we already know that list comprehension is used to produce the new list from the existing one. In list comprehension we have two things that are important Ans should be taken into consideration while using list comprehension in Haskell. Also, it is an in-built notion of Haskell so that we can use it directly. So we do not require to use any external dependency or any import statement to use this list comp in Haskell. In this section we will see its internal working and first we will see how to use this and what things include to make it work see below;
Let’s take a sample syntax or piece of code to understand its working in detail see below;
Example:
[a^2 | a <- [1..10]]
1) a <- [1..10]: This art of the list comprehension is said to be the generator for us. As the name suggests this generator is used to generate the values of the ‘a’ variable here. We can write or use any way to generate the values, or we can simply assign the list or array as well here. It is not restricted to use one generator only, we can have multiple generators separated by the ‘,’ comma itself. Now we will see the syntax for the multiple generators written inside the list comprehension in Haskell see below;
2) Multiple generators in List comprehension: This multiple generator can be produced using the ‘,’ we can have any number of the generator inside the list comprehension in Haskell. Also, let’s see its syntax for better understanding ;
Example:
[(a,b) | a <- [5,6,7], b <- [1,2,3]]
As you can see in the above line of syntax we have used to generator here for the list comprehension in Haskell, it is very easy to use and handle as well. So now it will produce the new list which will contain the elements from both the generators and return us the new list as the result.
3) It is not always to have the same order of the generator inside the list comprehension, if we want different order of the list generated from the list comprehension then we can write it like the way we want. We can also swap their orders.
Now let’s take a sample piece of code written in Haskell which will better explain the internal working of list comprehension with the proper syntax for beginners to understand it see below;
Example:
main = do
print([(a,b) | a <- [5,6,7], b <- [1,2,3]])
As you can see in the above lines of code we have used list comprehension in Haskell. Here we have started the program by the main module of Haskell, immediately after that, we have written the list comprehension, which has two generators inside it to produce the result. The first generator will produce the values for the ‘a’ and the second generator will produce the value for ‘b’ and in the list comprehension, the output will be the mix of both the generator values. So as you can see it is very easy and now we can implement it with any Haskell function also.
Examples
1) In this example we are trying to use list comprehension in Haskell and trying to produce the new list based on the generates we have created. This is a sample example for beginners.
Example:
main = do
print("Demo example to show list comprehension in Haskell !!")
let list1 = [(a,b) | a <- [5,6,7], b <- [1,2,3]]
let list2 = [(a1,b1) | a1 <- [1..2], b1 <- [1..5]]
let list3 = [(a2,b2) | a2 <- [10, 30 , 40, 50], b2 <- [101, 201, 301, 401]]
let list4 = [(a) | a <- [1..10]]
let list5 = [(a,b) | a <- [11, 12, 13, 14], b <- [15, 16, 17]]
let list6 = [(z) | z <- [1]]
let list7 = [(i) | i <- [100, 400, 90, 800]]
print("Printing the result !!")
print("list one after comprehension :::", list1)
print("list two after comprehension :::", list2)
print("list three after comprehension :::", list3)
print("list four after comprehension :::", list4)
print("list five after comprehension :::", list5)
print("list six after comprehension :::", list6)
print("list seven after comprehension :::", list7)
Output:
Conclusion
List comprehension in Haskell is a way to produce the list of new elements from the generator we have passed inside it. Also for the generator values, we can apply the Haskell functions to modify it later. This list comprehension is very y easy to use and handle for developers and beginners as well.
Recommended Articles
We hope that this EDUCBA information on “Haskell list comprehension” was beneficial to you. You can view EDUCBA’s recommended articles for more information.