Skip to main content Accessibility help
Internet Explorer 11 is being discontinued by Microsoft in August 2021. If you have difficulties viewing the site on Internet Explorer 11 we recommend using a different browser such as Microsoft Edge, Google Chrome, Apple Safari or Mozilla Firefox.

Chapter 2: First steps

Chapter 2: First steps

pp. 14-21

Authors

, University of Nottingham
Resources available Unlock the full potential of this textbook with additional resources. There are Instructor restricted resources available for this textbook. Explore resources
  • Add bookmark
  • Cite
  • Share

Summary

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.

About the book

Access options

Review the options below to login to check your access.

Purchase options

eTextbook
US$47.00
Paperback
US$47.00

Have an access code?

To redeem an access code, please log in with your personal login.

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.

Also available to purchase from these educational ebook suppliers