Haskell Language List Comprehensions Basic List Comprehensions


Example

Haskell has list comprehensions, which are a lot like set comprehensions in math and similar implementations in imperative languages such as Python and JavaScript. At their most basic, list comprehensions take the following form.

[ x | x <- someList ]

For example

[ x | x <- [1..4] ]    -- [1,2,3,4]

Functions can be directly applied to x as well:

[ f x | x <- someList ]

This is equivalent to:

map f someList

Example:

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