This chapter covers
- Facilitating side effects and input/output within Haskell
- Using side-effect free, pure code within impure code
- Interacting with the operating systems environment
- Incorporating a command line argument parser into our programs
In the previous chapter we learned how to construct algorithms to transform strings and explored the basics of programming in Haskell. However, we were only able to test our functionality in GHCi. Now, we want to turn our attention to writing our first real program, which can be invoked from the command line.
When working on the command line or writing automated scripts on Unix-like systems often pipelines of multiple programs are used for achieving certain tasks like searching or transforming data. For facilitating inter-process communication easily, streams of texts are used to pass messages from one process to another. This is usually done with the pipe symbol |
. This idea is illustrated by figure 3.1.
For this to work many tools exist in the UNIX space that perform basic tasks which, when combined in a pipeline, perform a bigger action.
One of these tools is nl
which is short for "number lines". Its job is rather simple: Print a file and number each line. Essentially, it creates listings for arbitrary text. Here is an example: