1 Functions and types


This chapter covers

  • Using the Glasgow Haskell Compiler (GHC) interpreter to solve problems
  • Writing simple functional programs with pure functions and I/O actions
  • Using a type-based approach to design programs
  • Using GHC extensions for greater code readability
  • Efficient processing of text data

Functional programming differs significantly from imperative programming in the ways we design programs. Typing discipline adds some specifics, too. When we code in Haskell, we think in a special way: in terms of the given data and the desired processing results (with both sides expressed by types), instead of focusing on the steps we should execute to get those results.

In this chapter, we’ll see several examples of how to solve problems in the most Haskellish way:

  • By using GHCi REPL (read-evaluate-print-loop) without writing a program
  • By writing functions properly
  • By keeping pure functions separate from the I/O actions that communicate to users
  • By expressing ideas with types

We’ll also explore several of Haskell’s libraries for text processing, which is arguably one of the most common, albeit routine, tasks in software development nowadays.

1.1 Solving problems in the GHCi REPL with functions

Suppose we want to analyze the vocabulary of a given text. Many sophisticated methods for such analysis are available, but we will do something quite basic, though still useful:

1.2 From GHCi and String to GHC and Text

1.3 Functional programs as sets of IO actions

1.4 Embracing pure functions

1.4.1 Separating I/O from pure functions

1.4.2 Computing the most frequent words by sorting them

1.4.3 Formatting reports

1.4.4 Rule them all with IO actions