A pager is a program that lets a user display text from a file one page at a time. The term “pager” isn’t used very frequently these days, but if you’ve ever used common command-line programs like less or man you’ve used a pager.

A pager is a great example of a small utility that will let you apply all of the different concepts that you’ve been learning about to build a single complete application that does something recognizable while still being small enough to start and finish as you work through this chapter.

Fundamentally, our pager needs to let us do three things:

  1. Output the contents of an ASCII or UTF8 encoded text file to the screen
  2. Scroll the output backwards and forwards one page at a time
  3. Display some metadata, like the file name and creation time

Setting Up the Application

We’ll start working on our application by creating a new project that has both a library, where most of our logic will be, and an executable that will call out to the library. There are a few additional dependencies we’ll need as we’re working through this application. You can add the dependencies now and we’ll look at them in more detail as we start to make use of them throughout the chapter.

​  cabal-version: 2.4
​  name: hcat
​  version: 0.1.0.0
​  library
​  hs-source-dirs: src
​  exposed-modules: HCat
​  build-depends: base
​  , bytestring
​  , text
​  , process
​  , directory
​  , time
​  default-language: Haskell2010
​  
​  executable hcat
​  hs-source-dirs: app
​  main-is: Main.hs
​  build-depends: base, hcat
​  default-language: Haskell2010

Next, we need to create our basic application outline. That means we need to create the files that we’ve listed in our cabal project: src/HCat.hs and app/Main.hs .

We’ll start our library, since we will be importing it into our executable. For now, we’ll define a single function named runHCat . This function isn’t doing anything yet, but adding it now ensures that we can call something from Main and that will let us verify that our build environment is set up and working correctly before we start writing more code:

​  ​ module ​ ​ HCat ​ ​ where ​
​  
​  runHCat :: ​ IO ​ ()
​  runHCat = return ()

Finally, let’s write Main . Like HCat this module is going to be pretty empty. The only thing we want to to is to write a main function that calls runHCat :

​  ​ module ​ ​ Main ​ ​ where ​
​  ​ import ​ ​ HCat ​ (​ runHCat ​)
​  
​  main :: ​ IO ​ ()
​  main = ​ HCat ​.runHCat

Once you have all of the pieces in place, you should be able to compile and run your program with cabal run hcat . This will let you know that all of the pieces are in place and working.