Ranges and enumeration

Contents
Ranges
Step sizes
Infinite ranges
repeat, cycle, replicate

Ranges

Two dots between two values of an enumerable type produce the list of everything in between. The upper bound is included:

[1..5] -- [1,2,3,4,5] ['a'..'e'] -- "abcde" [1.0,1.5..3.0] -- [1.0,1.5,2.0,2.5,3.0]

A range needs an Enum instance, which numbers, characters, and booleans have. It works because the type knows how to walk from one value to the next.

Step sizes

Give two starting values and the range continues with the same difference. The third value is the upper bound, not the step:

[0,2..10] -- [0,2,4,6,8,10] [10,8..0] -- [10,8,6,4,2,0] [1,4..20] -- [1,4,7,10,13,16,19]

A step that never reaches the bound produces a short list rather than an error: [1,3..10] ends at 9. A descending range with a positive step is empty, which is the usual cause of a function that silently returns nothing.

Infinite ranges

Leave the upper bound out and the list never ends. Because Haskell is lazy, nothing is computed until a consumer asks:

take 5 [1..] -- [1,2,3,4,5] take 4 [0,5..] -- [0,5,10,15] takeWhile (< 100) (map (^2) [1..]) -- [1,4,9,16,25,36,49,64,81]

take and takeWhile are the two standard ways to cut an infinite list down to size. A bare infinite range printed at the prompt runs until you press Ctrl+C.

repeat, cycle, replicate

Three generators cover the cases that a range cannot express. repeat never stops, cycle loops a finite list, and replicate counts:

take 3 (repeat 7) -- [7,7,7] take 5 (cycle [1,2]) -- [1,2,1,2,1] replicate 3 "ha" -- ["ha","ha","ha"]

concat (replicate 3 "ha") builds a string from repeated pieces, which is the usual reason to reach for replicate.

Next: List comprehensions

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

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: