Ranges and enumeration
The range syntax
Square brackets with two dots build a list from a first value to a last one, inclusive on both ends. The values are not integers only: any type whose values can be enumerated and compared works, which in practice means numbers and characters.
module Main (main) where
main :: IO ()
main = do
print ([1 .. 10] :: [Int])
print ([1, 3 .. 9] :: [Int])
print ([10, 9 .. 1] :: [Int])
print (['a' .. 'e'] :: [Char])
print (take 5 [1 ..] :: [Int])
print (take 4 [2, 4 ..] :: [Int])
ghc -Wall -O1 -o ranges ranges.hs
./ranges
[1,2,3,4,5,6,7,8,9,10]
[1,3,5,7,9]
[10,9,8,7,6,5,4,3,2,1]
"abcde"
[1,2,3,4,5]
[2,4,6,8]
A range of characters prints as a string, because [Char] is String and print shows a string with quotes. The last two lines have no upper bound: [1 ..] is the list of every Int from one upwards, and it is only usable because take stops asking for elements.
A step, not a count
Two values before the dots give the step, not the number of elements: [1, 3 .. 9] means start at 1, move by 3 - 1 = 2, and stop at the last value that does not pass 9. The upper bound is a limit, not an element to be produced: a step that does not land on it never prints it.
[1, 3 .. 9] -- 1, 3, 5, 7, 9
[1, 3 .. 10] -- 1, 3, 5, 7, 9, the 10 is never reached
[10, 9 .. 1] -- descending, because the step is negative
[5, 5 .. 9] -- an infinite list of 5s, the step is zero
The third line shows the usual mistake in reverse: a descending range needs a descending step, and [1 .. 10] with a negative step is the empty list, not an error. The fourth line is the trap that has no warning of its own: a zero step enumerates forever, so length [5, 5 .. 9] does not return.
Infinite ranges and take
A range with no upper bound is a perfectly good value: it is a list whose tail is computed only when somebody asks for it, which is what laziness means in practice. The program above prints five elements of [1 ..] and four of [2, 4 ..] because take answers before the rest of the list exists.
take 5 [1 ..] -- [1,2,3,4,5]
take 4 [2, 4 ..] -- [2,4,6,8]
length [1 ..] -- does not return
sum (take 10 [1 ..]) -- 55
The third line is the one to remember: a function that needs the whole list (length, reverse, sort, print) never finishes on an infinite list, while one that consumes a prefix (take, head, elem when the answer comes early) does. The lesson on laziness comes back to this; here the rule is enough: an unbounded range is safe only under a function that stops.
enumFromTo and friends
The bracket syntax is sugar for four Prelude functions, one per shape. They are worth knowing because a library may name them and because they make the step rule explicit:
module Main (main) where
main :: IO ()
main = do
print (enumFromTo 1 5 :: [Int])
print (enumFromThenTo 1 3 9 :: [Int])
print (enumFromThenTo 10 9 1 :: [Int])
print (take 5 (enumFrom 1) :: [Int])
print (take 4 (enumFromThen 10 8) :: [Int])
ghc -Wall -O1 -o enums enums.hs
./enums
[1,2,3,4,5]
[1,3,5,7,9]
[10,9,8,7,6,5,4,3,2,1]
[1,2,3,4,5]
[10,8,6,4]
The five lines match the four forms: [a .. b] is enumFromTo a b, [a, b .. c] is enumFromThenTo a b c, [a ..] is enumFrom a and [a, b ..] is enumFromThen a b. The last two forms have no end, so the program takes a prefix; without take the run would never finish.
Floats in a range
A range over Double is allowed, and it is the one place where a beginner can be surprised by a value that looks wrong and is not: the step is computed in binary floating point, so a decimal step such as 0.1 is not exactly representable and the elements drift.
module Main (main) where
main :: IO ()
main = do
print ([0, 0.1 .. 0.5] :: [Double])
print (length ([0, 0.1 .. 0.5] :: [Double]))
ghc -Wall -O1 -o floats floats.hs
./floats
[0.0,0.1,0.2,0.30000000000000004,0.4,0.5]
6
The fourth element is the one to look at: 0.30000000000000004 is the closest Double to the sum of three steps of 0.1, and the length is 6 even though the bound is 0.5, because the accumulated value is still below the bound when the sixth element is produced. The lesson on numbers explains why; the practical rule is that a range of floats is for display and not for arithmetic, and that a counted loop belongs to take n over an integer range.
Exercises
- Print the even numbers from 2 to 20 with a step of two, then the same list with filter even [2 .. 20].
- Predict the output of [1, 3 .. 10] and check it.
- Build [10, 9 .. 1] and explain why [1 .. 10] with a negative step is empty.
- Use take 3 (enumFromThen 1 4) and name the function the bracket syntax stands for.
- Print the letters "aeiou" as a list of characters built from a range, then join them with ++.
- Sum the first ten elements of [1 ..] and explain why the program terminates.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
[1, 3 .. 9]evaluate to?Answer
[1,3,5,7,9]
Two values give the step of two, and the bound 9 is reached exactly, so the range stops there.
-
What does
[1, 3 .. 10]evaluate to?Answer
[1,3,5,7,9]
The bound is a limit, not an element: the step of two lands on 9 and the next value 11 passes 10.
-
What does this program print?
module Main (main) where main :: IO () main = print (take 4 [2, 4 ..] :: [Int])Answer
[2,4,6,8]
The range starts at 2 with a step of 2 and has no bound; take asks for four elements and stops.
-
Which call does not return?
Answer
length [1 ..]length must walk the whole list, and an unbounded range has no last element to stop at.
-
What is the step of
[10, 8 .. 2]?Answer
-2
The step is the second value minus the first: 8 - 10 = -2, so the range descends and stops at 2.
-
What does
[1 .. 10]with a step of -1 give?Answer
The empty list
The start 1 is already past the bound 10 in the direction of a negative step, so no element is produced.
-
Which Prelude function does
[1, 3 .. 9]call?Answer
enumFromThenTo 1 3 9Two values before the dots and a bound map to enumFromThenTo; one value and a bound would be enumFromTo.
-
What does this program print?
module Main (main) where main :: IO () main = print (['a' .. 'e'] :: [Char])Answer
"abcde"
[Char] is String, and print shows a string with quotes, so the output is "abcde".
-
What does
length ([0, 0.1 .. 0.5] :: [Double])return?Answer
6
The drift of the binary step leaves the sixth element below the bound, so six elements are produced.
-
Why is
[5, 5 .. 9]dangerous?Answer
The step is zero, so the range is infinite
The step is 5 - 5 = 0, so every element equals 5 and the bound 9 is never passed.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev