Unit 3. Programming in types
Types in Haskell constitute their own way of writing programs. Unit 1 introduced you to the general ideas of functional programming. If this was your first introduction to functional programming, you were likely presented with an entirely new way of thinking about writing code and solving problems. Haskell’s type system is so powerful that it’s best to approach it as a second programming language that works in conjunction with what you learned in unit 1.
But what does it mean to think about programming in types? When you look at the type signature of a function—say, a -> b—you can view this signature as a description of a transformation. Suppose you have a type signature CoffeeBeans -> CoffeeGrounds; what function could this possibly describe? Knowing no more than those two types, you could guess that this function is grind. What about CoffeeGrounds -> Water -> Coffee? Clearly, this is the brew function. Types in Haskell allow you to view programs as a series of transformations.
You can think of transformations as a more abstract level of thinking about functions. When solving problems in Haskell, you can approach them first as a sequence of abstract transformations. For example, say you have a large text document and you need to find all the numbers in that document and then add them all together. How are you going to solve this problem in Haskell? Let’s start with knowing that a document can be represented as a String: