In this chapter we take our first proper steps with Haskell. We start by introducing the GHC system and the standard prelude, then explain the notation for function application, develop our first Haskell script, and conclude by discussing a number of syntactic conventions concerning scripts.
Glasgow Haskell Compiler
As we saw in the previous chapter, small Haskell programs can be executed by hand. In practice, however, we usually require a system that can execute programs automatically. In this book we use the Glasgow Haskell Compiler, a state-of-the-art, open source implementation of Haskell.
The system has two main components: a batch compiler called GHCi, and an interactive interpreter called GHCi. We will primarily use the interpreter in this book, as its interactive nature makes it well suited for teaching and prototyping purposes, and its performance is sufficient for most of our applications. However, if greater performance or a stand-alone executable version of a Haskell program is required, the compiler itself can be used. For example, we will use the compiler in extended programming examples in chapters 9 and 11.
Installing and starting
The Glasgow Haskell Compiler is freely available for a range of operating systems from the Haskell home page, http://www.haskell.org. For first time users we recommend downloading the Haskell Platform, which provides a convenient means to install the system and a collection of commonly used libraries. More advanced users may prefer to install the system and libraries manually.
Once installed, the interactive GHCi system can be started from the terminal command prompt, such as $, by simply typing ghci:
All being well, a welcome message will then be displayed:
GHCi, version A.B.C: http://www.haskell.org/ghc/ :? for help Prelude >
The GHCi prompt > indicates that the system is now waiting for the user to enter an expression to be evaluated. For example, it can be used as a calculator to evaluate simple numeric expressions:
> sqrt (3^2 + 4^2)
Following normal mathematical convention, in Haskell exponentiation is assumed to have higher priority than multiplication and division, which in turn have higher priority than addition and subtraction.