-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlecture-01.lhs
189 lines (112 loc) · 4.13 KB
/
lecture-01.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
University of Zagreb
Faculty of Electrical Engineering and Computing
PROGRAMMING IN HASKELL
Academic Year 2017/2018
LECTURE 1: Getting started
v1.1
(c) 2017 Jan Šnajder
==============================================================================
> import Data.Char
> import Data.List
=== THE BASICS ===============================================================
* Your favorite editor
* ghci
* program as a sequence of value/function definitions
* literate programming
* ghci commands
=== DEFINING VALUES AND FUNCTIONS ============================================
> x = 2
Functions are also values, so we can define them similarly. There are no
parentheses surrounding the variable:
> inc x = x + 1
Functions of many variables:
> digits2Number x y = x * 10 + y
So, don't write 'digits2Number(x,y)'. That is soooo non-Haskell!
We can now apply these functions. Again, the parentheses are dropped:
> y = inc 2
> z = digits2Number 4 2
Function names should be written with the initial letter in lowercase. Other
than that, the usual rules for identifiers apply.
Some built in functions: 'max', 'min', 'succ', 'div', 'mod'.
Infix format:
> w = 25 `div` 2
Note that, when you define values/functions in the interactive interpreter, you
have to put 'let' in front:
--> let x = 2
--> let inc x = x + 1
Why this is so will be clear by the end of the course.
=== STRINGS AND CHARACTERS ===================================================
> name = "Humpty Dumpty"
> letter = 'H'
Concatenating strings:
> s = "One " ++ "two " ++ "three"
You cannot concatenate letters! This won't work:
'a' ++ 'b'
Length will give you the length of a string:
> n1 = length "The quick brown fox jumps over the lazy dog"
> n2 = length s
=== IF-THEN-ELSE =============================================================
> condDec x = if x > 0 then x - 1 else x
> foo x = (if even x then x*2 else 2) + 1
Not the same as:
> foo' x = if even x then x*2 else 2 + 1
> bigNumber x = if x >= 1000 then True else False
Avoid explicitly returning True/False; instead, simply return the whole Boolean
expression.
> bigNumber' x = x >= 1000
Playing with strings a bit:
> merge s1 s2 =
> s1 ++ (if s1 < s2 then " is not " else " is ") ++ s2
> merge2 s1 s2 =
> s1 ++ " is " ++ (if s1 < s2 then "not " else "") ++ s2
=== GUARDS ===================================================================
> merge3 s1 s2
> | s1 < s2 = s1 ++ " is not " ++ s2
> | otherwise = s1 ++ " is " ++ s2
> grade score
> | score < 50 = 1
> | score < 63 = 2
> | score < 76 = 3
> | score < 89 = 4
> | otherwise = 5
> showSalary amount bonus
> | bonus /= 0 = "Salary is " ++ show amount ++ ", and a bonus " ++
> show bonus
> | otherwise = "Salary is " ++ show amount
=== EXERCISE 1 ===============================================================
1.1.
- Define 'concat3' that concatenates three strings, but drops the middle one
if it's shorter than 2 characters (use 'length' function).
1.2.
- Give a simpler definition of 'showSalary', using only one if-then-else
construct.
- Additionally check that salary is non-negative. If it's negative, return an
adequate message.
=== LISTS ====================================================================
> l1 = [1, 2, 3]
Operator ':' (so-called "cons"):
> l1' = (1:(2:(3:[])))
> l1'' = 1:2:3:[]
List concatenation:
> l2 = [1,2,3] ++ [4,5,6]
> myConcat l1 l2 = l1 ++ l2
Turning elements into singleton lists:
> listify x = [x]
> listify' x = x:[]
Extracting parts of a list: head, tail, init, last.
Taking or dropping the initial part of a list:
> l3 = take 3 [9,2,10,3,4]
> l4 = drop 3 [9,2,10,3,4]
Reversing a list:
> l5 = reverse [1,2,3]
Strings are lists of characters:
> l6 = "this is a list"
> l7 = head l6
> l8 = 'H' : "askell"
Is a string a palindrome?
> isPalindrome s = s == reverse s
=== NEXT =====================================================================
Next week we'll have two classes, to bring you up to speed as soon as possible.
In the first class, we'll continue talking about lists an tuples.
To prepare, you may (but need not to) read Chapter 2 of LYH:
http://learnyouahaskell.com/starting-out