Unit 5. Working with type in a context

 

In this unit, you’ll take a look at three of Haskell’s most powerful and often most confusing type classes: Functor, Applicative, and Monad. These type classes have funny names but a relatively straightforward purpose. Each one builds on the other to allow you to work in contexts such as IO. In unit 4, you made heavy use of the Monad type class to work in IO. In this unit, you’ll get a much deeper understanding of how that works. To get a better feel for what these abstract type classes are doing, you’ll explore types as though they were shapes.

One way to understand functions is as a means of transforming one type into another. Let’s visualize two types as two shapes, a circle and a square, as shown in figure 1.

Figure 1. A circle and square visually representing two types

These shapes can represent any two types, Int and Double, String and Text, Name and FirstName, and so forth. When you want to transform a circle into a square, you use a function. You can visualize a function as a connector between two shapes, as shown in figure 2.

Figure 2. A function can transform a circle to a square.

This connector can represent any function from one type to another. This shape could represent (Int -> Double), (String -> Text), (Name -> FirstName), and so forth. When you want to apply a transformation, you can visualize placing your connector between the initial shape (in this case, a circle) and the desired shape (a square); see figure 3.