List comprehensions

Contents
The form
Filters
Several generators
let inside a comprehension

The form

A comprehension describes a list by what its elements look like and where they come from. Read the vertical bar as the words for each:

[x * 2 | x <- [1..5]] -- [2,4,6,8,10] [toUpper c | c <- "hello"] -- "HELLO"

The left part is an expression in terms of the bound variable, and the right part is a generator x <- list. It is the same idea as map, with more room for conditions.

Filters

Extra conditions after the generator keep only the elements that satisfy them. Several conditions are separated by commas and all must hold:

[x | x <- [1..20], even x] -- [2,4,6,8,10,12,14,16,18,20] [x | x <- [1..50], even x, x < 10] -- [2,4,6,8] [(x, y) | x <- [1,2], y <- ["a","b"]] -- [(1,"a"),(1,"b"),(2,"a"),(2,"b")]

The last example shows that a comprehension can produce tuples, which makes it a compact way to build a small table.

Several generators

Two generators nest like two loops, and the later one may depend on the earlier one. That dependency is the reason to prefer a comprehension over concatMap:

[ (x, y) | x <- [1..3], y <- [x..3] ] -- [(1,1),(1,2),(1,3),(2,2),(2,3),(3,3)]

The order matters: the leftmost generator changes slowest, exactly like the outer loop of a nested for.

let inside a comprehension

A let line names an intermediate value for the rest of the comprehension. It cannot filter, only name:

[ scaled | x <- [1..5] , let scaled = x * x , scaled > 4 ] -- [9,16,25]

When a comprehension grows past four lines, rewrite it as a do block over lists, which lesson 32 covers. The two are the same construct with different punctuation.

Next: Recursion replaces loops

Article author: Arthur Isaev

Related articles
Haskell
Haskell course: zero to hero in 50 lessons
What is Haskell and why it matters
Install the toolchain with GHCup
GHCi: the interactive shell
Your first program
Values with let and where
Types and type signatures
Functions and application
Currying and partial application
Operators, sections, and fixity
if, guards, and pattern guards
case expressions
Pattern matching
Tuples
Lists
Ranges and enumeration
List comprehensions
Recursion replaces loops
map, filter, and foldr
Lambdas and composition
Type classes
Parametric polymorphism
Maybe and null safety
Either and recoverable errors
data: product types
data: sum types
Records
newtype and type synonyms
Recursive data: binary trees
Functor
Applicative
Monad
do notation
IO and purity
Files and IO
Modules and imports
Cabal projects
Dependencies
Testing with HUnit
Property testing with QuickCheck
Laziness and infinite lists
Strictness and space leaks
Folds
Monoids
Traversable
Monad transformers
Errors and exceptions
Concurrency
Parsing with Megaparsec
Capstone: a todo CLI
Hero roadmap: the whole course on one page
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: