liveBook · Manning

Enum

As explained in Learn Haskell by Example

An enumeration type is an algebraic data type whose constructors are all nullary and whose values form a finite enumerable set.

An enumeration type contains only nullary constructors, so its finite set of values can be traversed and mapped to numeric positions. Deriving Enum supplies that behavior automatically. section 15.3.3

Use Enum to work with constructor order and ranges:

  • Derive Bounded as well to obtain minBound and maxBound for the complete range. section 15.3.3

Enumerating file types

data FileType = Other | Image | Compressed deriving (Show, Read, Eq, Enum, Bounded) allFileTypes :: [FileType] allFileTypes = enumFromTo minBound maxBound The helper returns [Other, Image, Compressed] without manually repeating the first and last constructors.

How Learn Haskell by Example covers it

How deep it goes
Defines Enum methods and derived behavior, but stops short of implementation internals.
Examples
Uses EnumType with A, B, and C plus FileType with Other, Image, and Compressed.
What it assumes
Assumes algebraic data type declarations and deriving syntax, while demonstrating enumeration behavior from basic examples.
What you can do after
Apply it via derived Enum and Bounded instances to build complete constructor lists and numeric mappings.
How it connects
Connects enumeration types with algebraic data types, type classes, list ranges, and Bounded.

Also explained in

Compare how 23 books explain enum →

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.