Professional Documents
Culture Documents
02 First Steps
02 First Steps
02 First Steps
0
The Hugs System
www.haskell.org/hugs
1
Starting Hugs
% hugs
>
2
The Hugs > prompt means that the Hugs system
is ready to evaluate an expression.
For example:
> 2+3*4
14
> (2+3)*4
20
3
The Standard Prelude
4
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
9
Moreover, function application is assumed to have
higher priority than all other operators.
f a + b
10
Examples
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
11
Haskell Scripts
12
My First Script
double x = x + x
% hugs test.hs
> quadruple 10
40
Note:
> :reload
Reading file "test.hs"
> factorial 10
3628800
16
Naming Requirements
xs ns nss
17
The Layout Rule
a = 10 a = 10 a = 10
b = 20 b = 20 b = 20
c = 30 c = 30 c = 30
18
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
Command Meaning
20
Exercises
N = a ’div’ length xs
where
a = 10
xs = [1,2,3,4,5]
21
(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.
22