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:
$ ghchi
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:
> 2+3*4
14
> (2+3)*4
20
> sqrt (3^2 + 4^2)
5.0
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.
Review the options below to login to check your access.
Log in with your Cambridge Aspire website account to check access.
If you believe you should have access to this content, please contact your institutional librarian or consult our FAQ page for further information about accessing our content.