CH 2
CH 2
1
Glasgow Haskell Compiler
2
Starting GHCi
Prelude>
> 2+3*4
14
> (2+3)*4
20
4
The Standard Prelude
5
Remove the first element from a list:
> [1,2,3,4,5] !! 2
3
Reverse a list:
f(a,b) + c d
f a b + c*d
10
Moreover, function application is assumed
to have higher priority than all other
operators.
fa+b
11
Examples
Mathematics Haskell
f(x) fx
f(x,y) fxy
f(g(x)) f (g x)
f(x,g(y)) f x (g y)
f(x)g(y) fx*gy
12
Haskell Scripts
$ ghci test.hs
> quadruple 10
40
Note:
> :reload
Reading file "test.hs"
> factorial 10
3628800
17
Command
Useful GHCi Meaning
Commands
: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
18
commands
Naming Requirements
19
The Layout Rule
a = 10 a = 10 a = 10
b = 20 b = 20 b = 20
c = 30 c = 30 c = 30
20
The layout rule avoids the need for explicit
syntax to indicate the grouping of
definitions.
a=b+c a=b+c
where where
b=1 means {b = 1;
c=2 c = 2}
d=a*2 d=a*2
implicit explicit
grouping grouping
21
Exercises
Try out slides 2-7 and 13-16 using
(1) GHCi.
N = a ’div’ length xs
where
a = 10
xs = [1,2,3,4,5]
22
(3) Show how the library function last that
selects the last element of a list can be
defined using the functions introduced
in this lecture.
23