List Comprehensions

List Comprehensions

There is an alternative way to define map and filter, which is to use list comprehension.

map f xs    = [f x | x <- xs]
filter p xs = [x | x <- xs, p x]

With list comprehension we can also do things like:

> [x + y | x <- [1..2], y<- [1..3]]
[2,3,4,3,4,5]

-- .. is syntax sugar for range
> [1..10]
[1,2,3,4,5,6,7,8,9,10]

> ['a' .. 'z']
"abcdefghijklmnopqrstuvwxyz"