liveBook · Manning

Range

As explained in Learn Haskell by Example

Range syntax is Haskell list notation that denotes enumeration of values between specified bounds or from a starting value.

A range is Haskell’s compact list notation for enumerating values between bounds, or continuing from a starting value. Write [start .. end] for consecutive values, or [start, next .. end] to specify a step. section 2.3.1 section 15.3.3

Use ranges with numeric values and characters:

  • An omitted end creates an open-ended range, as in [1 ..], while a specified end limits the result. section 15.3.3

Enumerating a custom type

Ranges also work with enumeration types. A type such as data EnumType = A | B | C deriving (Show, Enum) supports [A .. C], which produces [A,B,C]. The notation corresponds to enumFromTo; deriving Bounded additionally allows enumFromTo minBound maxBound to enumerate every constructor without naming the endpoints.

How Learn Haskell by Example covers it

How deep it goes
Defines bounded and open-ended notation, stepped ranges, and its expansion into Enum functions without detailing evaluation internals.
Examples
Builds alphabets, digit collections, numeric sequences, and complete lists of custom FileType constructors.
What it assumes
Assumes basic Haskell list notation and types while introducing enumeration through Enum and Bounded.
What you can do after
Apply range notation to construct bounded, stepped, open-ended, and custom enumeration lists.
How it connects
Connects list notation with Enum, Bounded, Char, Int, and type synonyms.

Also explained in

Compare how 8 books explain range →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.