installation-guide
installation-guide
Two main components: a batch compiler called GHC, and an interactive interpreter called
GHCi.
***We will primarily use the interpreter, as its interactive nature makes it well suited for
teaching purposes, and its performance is sufficient for most of our applications.
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 download 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:
$ ghci
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
Standard prelude
Haskell comes with a large number of built-in functions, which are defined in a library file called
the standard prelude. In addition to familiar numeric functions such as + and *, the prelude also
provides a range of useful functions that operate on lists. In Haskell, the elements of a list are
enclosed in square parentheses and are separated by commas, as in [1, 2, 3, 4, 5]. Some of the
most commonly used library functions on lists are illustrated below.
Reverse a list:
> reverse [1, 2, 3, 4, 5]
[5, 4, 3, 2, 1]
Haskell scripts
As well as the functions provided in the standard prelude, it is also possible to define new
functions.
New functions are defined in a script, a text file comprising a sequence of definitions. By
convention,
Haskell scripts usually have a .hs suffix on their filename to differentiate them from other kinds
of files.
This is not mandatory, but is useful for identification purposes.
My first script
When developing a Haskell script, it is useful to keep two windows open, one running an editor
for the script, and the other running GHCi. As an example, suppose that we start a text editor
and type in the following two function definitions, and save the script to a file called test.hs:
double x = x + x quadruple x = double (double x) In turn, suppose that we leave the editor open,
and in another window start up the GHCi system and instruct it to load the new script:
$ ghci test.hs
Now both the standard prelude and the script test.hs are loaded, and functions from both can
be freely used. For example:
> quadruple 10
40
Now suppose that we leave GHCi open, return to the editor, add the following two function
definitions to those already typed in, and resave the file:
factorial n = product [1..n]
average ns = sum ns ‘div‘ length ns
We could also have defined average ns = div (sum ns) (length ns), but writing div between its
two arguments is more natural. In general, any function with two arguments can be written
between its arguments by enclosing the name of the function in single back quotes ‘ ‘.
Note that any command can be abbreviated by its first character. For example :load can be
abbreviated by :l. The command: set editor is used to set the text editor that is used by the
system. For example, if you wish to use vim you would enter :set editor vim.