0% found this document useful (0 votes)
2 views

installation-guide

This installation guide provides instructions for setting up the Glasgow Haskell Compiler (GHC) and its interactive interpreter GHCi on Windows 10. Users are encouraged to download the Haskell Platform for an easy installation, and the guide includes examples of using GHCi for calculations and list operations. Additionally, it explains how to create and load Haskell scripts, define new functions, and use GHCi commands effectively.

Uploaded by

varzy2305
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

installation-guide

This installation guide provides instructions for setting up the Glasgow Haskell Compiler (GHC) and its interactive interpreter GHCi on Windows 10. Users are encouraged to download the Haskell Platform for an easy installation, and the guide includes examples of using GHCi for calculations and list operations. Additionally, it explains how to create and load Haskell scripts, define new functions, and use GHCi commands effectively.

Uploaded by

varzy2305
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Installation Guide

(Minimum requirement: Windows 10)

Glasgow Haskell Compiler

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.

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 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

***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. For example, 2*3^4 means 2*(3^4), while 2+3*4 means 2 + (3*4).

Functional Programming Page 1


Moreover, exponentiation associates (or brackets) to the right, while the other four main
arithmetic operators associate to the left. For example, 2^3^4 means 2 ^(3^4), while 2-3+4
means (2-3)+4. In practice, however, it is often clearer to use explicit parentheses in such
expressions, rather than relying on the above rules.

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.

Select the first element of a non-empty list:


> head [1, 2, 3, 4, 5]
1
Remove the first element from a non-empty list:
> tail [1, 2, 3, 4, 5]
[2, 3, 4, 5]

Select the nth element of list (counting from zero):


> [1, 2, 3, 4, 5] !! 2
3

Select the first n elements of a list:


> take 3 [1, 2, 3, 4, 5]
[1, 2, 3]

Remove the first n elements from a list:


> drop 3 [1, 2, 3, 4, 5]
[4,5]

Calculate the length of a list:


> length [1,2,3,4,5]
5

Calculate the sum of a list of numbers:


> sum [1, 2, 3, 4, 5]
15

Calculate the product of a list of numbers:


> product [1,2,3,4,5]
120

Functional Programming Page 2


Append two lists:
> [1, 2, 3] ++ [4, 5]
[1, 2, 3, 4, 5]

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

> take (double 2) [1, 2, 3, 4, 5]


[1, 2, 3, 4]

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 ‘ ‘.

Functional Programming Page 3


GHCi does not automatically reload scripts when they are modified, so a reload command must
be executed before the new definitions can be used:
> :reload
> factorial 10
3628800
> average [1,2,3,4,5]
3

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.

Figure 2.1 Useful GHCi commands

Functional Programming Page 4

You might also like