List Comprehension
Table of Contents
1 List Comprehension
1.1 Simple List Comprehension
> [x^2 | x <- [1..10]] [1,4,9,16,25,36,49,64,81,100] > [ odd x | x <- [1..9]] [True,False,True,False,True,False,True,False,True]
1.2 Comprehensions with multiple generators
Comprehensions with multiple generators, separated by commas. The generators are x <- [1, 2, 4] and y <- [4,5].
> [(x, y) | x <- [1, 2, 4], y <- [4,5]] [(1,4),(1,5),(2,4),(2,5),(4,4),(4,5)] > [(x-y, x+y) | x <- [1, 2, 4], y <- [4,5]] [(-3,5),(-4,6),(-2,6),(-3,7),(0,8),(-1,9)] > [(x/y, x*y) | x <- [1, 2, 4], y <- [4,5]] [(0.25,4.0),(0.2,5.0),(0.5,8.0),(0.4,10.0),(1.0,16.0),(0.8,20.0)]
1.3 Function Inside List Comprehension
> let f x y = sqrt(x^2 + y^2) > [ f x y | x <- [1, 2, 4], y <- [4,5]] [4.123105625617661,5.0990195135927845,4.47213595499958,5.385164807134504,5.656854249492381,6.4031242374328485]
1.4 Comprehension with Guards
Guards or filter is a boolean expression that removes elements that would otherwise have been included in the list comprehension. They restricts the values produced by earlier generators.
Even number sequence
> [x | x <- [1..10], even x] [2,4,6,8,10] > [x | x <- [1,5,12,3,23,11,7,2], x>10] [12,23,11] > [(x,y) | x <- [1,3,5], y <- [2,4,6], x<y] [(1,2),(1,4),(1,6),(3,4),(3,6),(5,6)]
Odd Number sequence
> [x | x <- [1..10], odd x] [1,3,5,7,9]
Number factors and Prime Numbers
> let factors n = [ x | x <- [1..n], mod n x == 0] > > factors 15 [1,3,5,15] > > factors 10 [1,2,5,10] > > factors 100 [1,2,4,5,10,20,25,50,100] > > factors 17 [1,17] > factors 19 [1,19] > let prime n = factors n == [1, n] > > prime 17 True > prime 19 True > prime 20 False > {- Get all prime numbers until number n -} > let primes_n n = [ x | x <- [1..n], prime x] > > primes_n 10 [2,3,5,7] > primes_n 100 [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97] >