-
Book Overview & Buying
-
Table Of Contents
Soar with Haskell
By :
We start with one of the simplest use cases of ADTs, which are often called enumerations, or enums for short. An enumeration type is a type with a finite number of distinct values. Many programming languages provide specific support for enums, but in Haskell, they are just a special case of ADTs and not especially distinguished from other ADTs.
We illustrate the use of enumeration types with the well-known game of rock-paper-scissors.
Rock-paper-scissors is a two-player game. There are three possible outcomes for the first player (and likewise for the second player) – lose, draw, or win:
data Outcome = Lose | Draw | Win
The data keyword signals that this is the definition of a new datatype. Next comes the name of the new datatype, Outcome. Finally, the possible values are enumerated: Lose, Draw, and Win. These values are called data constructors, value constructors, or constructors for short. Observe that both the names...