liveBook · Manning

Show

As explained in Learn Haskell by Example

The Show type class is a Haskell type class whose instances provide textual representations of values.

Show is a Haskell type class that provides show, converting values into String representations. GHCi uses show to display evaluated values. section 5.2.3

Use Show in practical code by:

  • Converting a numeric counter to text while recursively numbering lines: show counter ++ ". " ++ line. section 3.1.2
  • Deriving Show for custom data types when their field types also have Show instances. section 5.2.3
  • Remembering that derived output resembles Haskell source syntax rather than being intended as human-readable presentation. section 5.2.3
  • Pairing derived Show with Read when serialized constructor-like strings must be parsed back into values. section 15.3.2

Displaying a custom map

Declare newtype AssocMap k v = AssocMap [(k, v)] deriving (Show). Evaluating insert 'a' "World" (insert 'b' "Hello" empty) can then display as AssocMap [('b',"World"),('a',"Hello")]. section 5.2.3

data FileType
  = Other
  | Image
  | Compressed
  deriving (Show, Read, Eq)

See Show derived alongside Read and Eq for a custom FileType, enabling constructor-shaped display and parsing. section 15.3.2

How Learn Haskell by Example covers it

How deep it goes
Defines show, automatic derivation, instance constraints, and representation limits, but does not develop custom Show instances.
Examples
Uses numbered input lines, an AssocMap, and FileType values displayed or parsed through derived instances.
What it assumes
Assumes basic Haskell functions, strings, custom data types, and recursive IO while introducing type-class membership through Show.
What you can do after
Apply it via GHCi and deriving clauses to display counters and custom values as strings.
How it connects
Connects Show with type classes, Read, Eq, algebraic data types, deriving, GHCi, and recursive IO.

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.