List Comprehensions

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

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

With list comprehension we can also do things like:

  1. > [x + y | x <- [1..2], y<- [1..3]]
  2. [2,3,4,3,4,5]
  3. -- .. is syntax sugar for range
  4. > [1..10]
  5. [1,2,3,4,5,6,7,8,9,10]
  6. > ['a' .. 'z']
  7. "abcdefghijklmnopqrstuvwxyz"