Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

LIST COMPREHENSION

One of the distinct features of a functional language is the list comprehension notation, which
has no parallels in other paradigms.
In a list comprehension we write down a description of a list in terms of the elements of another
list.
List comprehension provides a concise method to generate and process lists. This syntactic
construct allows us to easily filter, map, or apply conditional logic on the list elements without
resorting to explicit recursion. Furthermore, this approach often presents a more readable and
understandable alternative to its recursive counterpart.
From the first list we generate elements. which we test and transform to form elements of the
result.
The list comprehension notation does not add any new programs to the Haskell language, but
it does allow us to (re-)write programs in a new and clearer way.
A list comprehension has the form:
[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.
(Or)

where each qualifier qi has one of two forms.


• It can be a generator, p <- lExp, where p is a pattern and lExp is an expression of list
type.
• It can be a test, bExp, which is a Boolean expression.
An expression lExp or bExp appearing in qualifier qi can refer to the variables used in the
patterns of qualifiers q1 to qi-1.
The expression on the left of the vertical bar is evaluated for each combination of generator
expressions on the right.
A generator expression binds a variable on the left , to an element of the list on the right.
List comprehensions are powerful and concise.
Ex:

As the preceding example shows, the combinations of generators are evaluated in depth first
order: for the first element of the first list, we evaluate every element of the second, and so on.
Example:[toUpper x | x <- ["hello world"]

Generator Expression:
As the name suggests this generator is used to generate the values of the variable mentioned.
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.

Example of Single Generator:


1. Suppose that the list ex is [2,4,7], then the list comprehension
[ 2*n | n <- ex] will be [4,8,14]
as it contains each of the element’s n of the list ex, doubled: 2*n.
We can read (1) as saying “Take all 2*n where n comes from ex”.
We can write the evaluation of the list comprehension in a table, thus:

2. In a similar way,
[ isEven n | n<-ex] yields [True,True,False]
if the function isEven has the definition
isEven :: Int -> Bool
isEven n = (mod n 2 == 0)
In list comprehensions n<-ex is called a generator because it generates the data from
which the results are built.
On the left-hand side of the '<-' there is a variable, n, while on the right-hand side we put
the list.
In this case ex, from which the elements are taken.

3. We can combine a generator with one or more tests, which are Boolean expressions, thus:
[ 2*n | n <- ex , isEven n , n>3 ] --------------- (2)
(2) is paraphrased as “Take all 2*n where n comes from ex, n is even and greater than
3.”

4. Instead of placing a variable to the left of the arrow '<-', we can put a pattern. For
instance,

5. We can add tests in such a situation, too:


Example of Multiple Generators:

Multiple generators can be produced using the ‘,’ we can have any number of the generator
inside the list comprehension in Haskell.

Multiple generators allow us to combine elements from two or more lists.

Example 1:

[(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.
Example 2:

This example is important as it shows the way in which the values x and y are chosen.
Demo:

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)

You might also like