CHAPTER 2 - FIRST STEPS

Glasgow Haskell Compiler

  • GHC is the leading implementation of Haskell, and comprises a compiler and interpreter;
  • The Interactive nature of the interpreter makes it well suited for teaching and prototyping;
  • GHC is freely available from this website
  • You may also use replit

STARTING GHCI

The interpreter can be started from the terminal command prompt $ by simply typing ghci

$ ghci
GHCi, version X: http://www.haskell.org/ghc/
:? for help
Prelude>

The GHCi prompt Prelude> indicates that the interpreter is now ready to evaluate an expression.

For example, it can be used as a desktop calculator to evaluate simple numeric expresions:

Prelude> 2+3*4
14
Prelude> (2+3)*4
20
Prelude> sqrt (3^2 + 4^2)
5.0
In [1]:
2+3*4
14
In [2]:
(2+3)*4
20
In [3]:
sqrt (3^2 + 4^2)
5.0

THE STANDARD PRELUDE

Haskell comes with a large number of standard library functions. In addition to the familiar numeric functions such as + and *, the library also provides many useful functions on LISTS.

Return the first element of a list (CAR!)

In [16]:
head [1,2,3,4,5]
1

Return the list after remove the first element from a list (CDR!)

In [17]:
tail [1,2,3,4,5]
[2,3,4,5]

Return the nth element of a list

In [20]:
[1,2,3,4,5] !! 2
3

Return the first n elements of a list

In [21]:
take 3 [1,2,3,4,5]
[1,2,3]

Return the list after removing the first n elements from a list

In [22]:
drop 3 [1,2,3,4,5]
[4,5]

Return the length of a list

In [23]:
length [1,2,3,4,5]
5

Return the sum of a list of numbers

In [24]:
sum [1,2,3,4,5]
15

Return the product of a list of numbers

In [25]:
product [1,2,3,4,5]
120

Append two lists:

In [26]:
[1,2,3] ++ [4,5]
[1,2,3,4,5]

Reverse a list

In [27]:
reverse [1,2,3,4,5]
[5,4,3,2,1]

FUNCTION APPLICATION

In Mathematics, function application is denoted using parentheses, and multiplication is often denoted using juxtaposition or space.

$f(a,b) + c d$

Apply the function f to a and b, and add the result to the product of c and d.

In Haskell, function application is denoted using space, and multiplication is denoted using *.

f a b + c*d

Moreover, function application is assumed to have higher priority than all other operators, so

f a + b means (f a) + b, rather than f (a + b).

Function application is left associative

In [9]:
double x = x + x
triple x = x + x + x
double triple 3
<interactive>:1:1: error:
    • Non type-variable argument in the constraint: Num (a -> a)
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        it :: forall a. (Num a, Num (a -> a)) => a
In [10]:
double (triple 3)
18

Some examples of function applications in Mathematics and the corresponding expressions in Haskell

Mathematics Haskell
$f(x)$ f x
$f(x, y)$ f x y
$f(g(x))$ f (g x)
$f(x, g(y))$ f x (g y)
$f(x) g(y)$ f x * g y

HASKELL SCRIPTS

In addition to using the functions defined in the standard Prelude, you can also define your own functions.

New functions are defined within a script, a text file comprising a sequence of definitions; By convention, Haskell scripts usually have a .hs suffix on their filename.

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.

Start an editor, type in the following two function definitions, and save the script as test.hs:

double x = x + x
quadruple x = double (double x)

Leaving the editor open, in another window start up GHCi with the new script:

ghci test.hs

Now both the standard library and the file test.hs are loaded, and functions from both can be used:

Prelude> quadruple 10
40
Prelude> take (double 2) [1,2,3,4,5,6]
[1,2,3,4]

Leaving GHCi open, return to the editor, add the following two definitions, and resave:

factorial n = product [1..n]
average ns = sum ns `div` length ns

Note:

  • div is enclose in back quotes, not forward;
  • x 'f' y is just syntactic sugar for f x y

GHCi does not automatically detect that the script has been changed, so a reload command must be executed before the new definitions can be used:

Prelude> :reload
Reading file "test.hs"
Prelude> factorial 10
3628800
Prelude> average [1,2,3,4,5]
3

Useful GHCi Commands

Command Meaning
:load name load script name
:reload reload current script
:set editor name set editor to name
:edit name edit script name
:edit edit current script
:type expr show type of expr
:? show all commands
:quit quit GHCi

Naming Requirements

  • Function and argument names must begin with a lower-case letter. For example:
myFun   fun1   arg_2   x’
  • By convention, list arguments usually have an s suffix on their name. For example:
xs   ns   nss

Haskell Keywords

case, class, data, default, deriving, do, else, foreign, if, import, in, infix, infixl, infixr, instance, let, module, newtype, of, then, type, where

The Layout Rule

In a sequence of definition, each definition must begin in precisely the same column:

Correct

a = 10
b = 20
c = 30

Incorrect

a = 10
 b = 20
c = 30

Incorrect

 a = 10
b = 20
 c = 30

The layout rule avoids the need for explicit syntax to indicate the grouping of definitions.

The following snippets are the same

-- Implicit grouping
a = b + c
    where
        b = 1
        c = 1
d = a * 2
-- explicit grouping
a = b + c
    where
        {b = 1;
        c = 1}
d = a * 2

Do not use TABs to indent.

Comments

In [2]:
-- This is a single line comment
x = 40  -- assign 40 to x
{-
Multi line comment
Multi line comment
-}
y = 60
x + y
100