Haskell List Comprehensions

  • Set builder
  • 
                [x | x <- e]              -- Python: [x for x in e]
                [x^2 | x <- e]            -- Python: [x**2 for x in e]
                [f e | x <- e]            -- Python: [f(x) for x in e]
                [x | x <- e, x > 2]       -- Python: [x for x in e if x > 2]       
                [x | x <-e, x > 1, x < 4] -- Python: [x for x in e if x > 1 if x < 4]       
    
                p :: a -> Bool            -- a boolean function
                [x | x <-e, p x]          -- Python: [x for x in e if p(x)]
    
                [x + y | x <- e, y <- f]  -- Python [x + y for x in e for y in f]          
                  -- this will return all paired-sum of elements in e and f