Custom type class example
Having studied a range of predefined type classes, we will finish this chapter with a worked example that declares a custom type class. This example builds on the frequency
function defined previously. The goal is, for a call such as frequency "banana"
, to not get the standard but boring Show
behavior. Instead, we want to get a more pleasant, human-readable format in tabular form, as follows:
┌─────┐
│a│■ ■ ■│
│b│■ │
│n│■ ■ │
└─────┘
To make our solution work for different types of keys, we will introduce a custom type class.
Text boxes
Our solution is based on a custom representation of text as rectangular boxes:
data Box = MkBox { content :: [String] ...