Lesson 14. Using type classes
After reading lesson 14, you’ll be able to
- Implement your own type classes
- Understand polymorphism in Haskell
- Know when to use deriving
- Search for documentation with Hackage and Hoogle
In lesson 13, you got your first look at type classes, which are Haskell’s way of grouping types by common behaviors they share. In this lesson, you’ll take a deeper look at how to implement existing type classes. This will allow you to write new types that take advantage of a wide range of existing functions.
Consider this
You have a data type consisting of data constructors for New England states:
You want to be able to display them by their full name by using Show. You can easily display their abbreviations by deriving show, but there’s no obvious way to create your own version of show. How can you make your NewEngland type display the full state name by using show?