2 Functional programming with types and type classes
This chapter covers:
- Writing simple functional programs with pure functions and I/O actions
- Using Glasgow Haskell Compiler (GHC) extensions for greater code readability and expressiveness
- Exploiting type classes as tools for writing code that works for values of different types
- Using basic type classes defined in the standard library
Functional programming differs significantly from imperative programming in the supported ways of designing programs. Typing discipline adds some specifics too. To be productive, it is vital to think differently when you program in Haskell. You should first think in terms of the given data and the desired results of processing (with both sides generally expressed by types), instead of focusing on the steps that should be executed to get those results.
Haskell programmers usually prefer to write code that can be reused in many ways. To achieve that you have to write code generically, meaning that you should not fix the particular types of the processed values. Instead, you provide a rather abstract definition of processing and then use language tools for concretizing it. You can do that in Haskell using type classes (for abstracting) and instances (for concretizing).