liveBook · Manning

Decimal

As explained in Learn Haskell by Example

decimal is an Attoparsec parser that reads a decimal integer value polymorphically over integral result types.

decimal is an Attoparsec parser with type Integral a => Parser a. It reads decimal integer input and produces a result in the requested integral type, such as Integer or Word16. section 12.2.2

Parsing PNM header dimensions

A PNM header parser uses C8.decimal for width and height, and optionally for maxVal. The surrounding parser skips whitespace and comments before each value. section 12.2.2

Listing 12.23 A parser for PNM headers with Attoparsec
headerP :: Parser Header
headerP = do
  skipWhitespace
  magicNumber <-
    choice                 #1
      [ magicNumberP1P,
        magicNumberP2P,
        magicNumberP3P,
        magicNumberP4P,
        magicNumberP5P,
        magicNumberP6P
      ]
  skipWhitespace
  width <- C8.decimal     #2
  skipWhitespace
  height <- C8.decimal   #2
  maxVal <- do
    if magicNumber == P1 || magicNumber == P4
      then return Nothing
      else Just <$> (skipWhitespace *> C8.decimal)     #3
  whitespace
  return Header {..}

See decimal used inside a complete Attoparsec parser for PNM headers, including polymorphic numeric fields and conditional maxVal parsing. section 12.2.2

How Learn Haskell by Example covers it

How deep it goes
Defines the parser type and demonstrates field-level use, without detailing decimal syntax or parser internals.
Examples
Uses decimal to parse PNM width, height, and optional maxVal values.
What you can do after
Apply it via Attoparsec to parse decimal integers into compatible integral result types.
How it connects
Connects decimal with Attoparsec, Parser, parser combinators, and the PNM format.

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.