Using Ad Hoc Polymorphism with Type classes
Before we dive into using type classes, let’s take a look at a small motivating example. Imagine that we wanted to write a function that could remove duplicate elements from a list. If we’re only making use of parametric polymorphism we’re a little bit limited in how we can write a function like this. To remove duplicates from the list we need to compare elements to find out if they match. The problem here is that our elements could be anything. We might even be dealing with functions or some other value that doesn’t have a regular notion of equality.
Your first thought might be to just use the (==) function that we’ve been making use of throughout this book, but as you’ll see shortly, the (==) function ...
Get Effective Haskell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.