An algebraic data type encodes a set of named alternatives as one user-defined type. In a command-line tool, it gives each supported option a clear representation that later functions can inspect and use to choose behavior. section 1.1.1 section 4.4.1
Listing 4.12 Data type used for representing options for the line number program
data LineNumberOption = ReverseNumbering #1 | SkipEmptyLines #2 | LeftAlign #3 deriving (Eq) #4
Defines three option constructors—ReverseNumbering, SkipEmptyLines, and LeftAlign—and derives Eq so options can be compared. section 4.4.1
A parser can convert ordinary strings into these constructors while using Maybe to represent failure. Once parsed, the options can influence control flow by selecting functions such as numberNonEmptyLines or numberAllLines. section 4.4.1
Listing 4.13 Function to parse normal strings into the new data type for options
lnOptionFromString :: String -> Maybe LineNumberOption lnOptionFromString "--reverse" = Just ReverseNumbering #1 lnOptionFromString "--skip-empty" = Just SkipEmptyLines #1 lnOptionFromString "--left-align" = Just LeftAlign #1 lnOptionFromString _ = Nothing #2
Maps recognized command-line strings to constructors and returns Nothing for unsupported options. section 4.4.1




