Professional Documents
Culture Documents
List_Comprehension
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)
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.
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,
Multiple generators can be produced using the ‘,’ we can have any number of the generator
inside the list comprehension in Haskell.
Example 1:
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)