Lesson 11 of 16
List Comprehensions
List Comprehensions
List comprehensions offer a concise syntax for building lists, inspired by set-builder notation:
[x * 2 | x <- [1..5]]
-- [2,4,6,8,10]
Read as: "for each x drawn from [1..5], produce x * 2".
With Guards
Add predicates after the generator:
[x | x <- [1..20], even x]
-- [2,4,6,8,10,12,14,16,18,20]
[x | x <- [1..30], x `mod` 3 == 0]
-- [3,6,9,12,15,18,21,24,27,30]
Multiple Generators
[(x, y) | x <- [1..3], y <- [1..3], x /= y]
-- [(1,2),(1,3),(2,1),(2,3),(3,1),(3,2)]
Your Task
Use a list comprehension to generate all perfect squares up to 100, then print them. (Squares: 1, 4, 9, ..., 100)
Haskell loading...
Loading...
Click "Run" to execute your code.