-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec-intro.hs
executable file
·369 lines (219 loc) · 6 KB
/
lec-intro.hs
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/env runhaskell
module Lec3 where
{-@ LIQUID "--short-names" @-}
{-@ LIQUID "--no-termination" @-}
{-@ LIQUID "--diff" @-}
import Prelude hiding (sum)
e1 :: Double
e1 = 31 * (42 + 56)
e2 = 3 * (4.2 + 5.6) :: Double
e3 :: Bool
e3 = True
e4 :: ((Int, Double), Bool)
e4 = ((7, 5.2), True)
zero :: Int
zero = 0
x = 12
pos :: Integer -> Bool
pos x = x > 0
-- pos 12 == 12 > 0 == True
pat3 :: Int -> Int -> Int -> Int
pat3 x y z = x * (y + z )
zog = pat3 2 10
pat :: (Int, Int, Int) -> Int
pat (x, y, z) = x * (y + z)
{-
mys ("cat", "dog", 49) ======> 49
mys (1, 2, 3) ======> 3
mys (True, False, "bob") ======> "bob"
-}
-- PATTERN MATCHING
mys :: (a, b, c) -> c
mys (x, y, z) = z
e5 = ['1', '2', 'c']
e6 :: [[Double]]
e6 = [[1], [2, 3], [4.2]]
emp = []
e7 :: [Int]
e7 = 1 : emp
e8 :: [String]
e8 = "cat" : emp
e9 :: [Double]
e9 = 4.9 : emp
cons2 :: a -> a -> [a] -> [a]
cons2 x1 x2 xs = x1 : x2 : xs
--------------------------------------------------------------------------------
-- | Get first element of list
--
-- >>> firstElem [1,2,3]
-- 1
--
-- >>> firstElem ["cat", "dog"]
-- "cat"
firstElem (x:_) = x
-- firstElem ["cat", "dog", "cat"]
--
-- >>> firstElem []
-- ???
--------------------------------------------------------------------------------
-- | Clone a value multiple times
--
-- >>> clone 4 "cat"
-- ["cat", "cat", "cat", "cat"]
--
-- >>> clone 3 'a'
-- "aaa"
--
-- >>> clone 1 3.14
-- [3.14]
-- >>> clone 0 3.14
-- []
clone :: Int -> t -> [t]
clone 0 x = []
clone n x = x : clone (n-1) x
(***) :: Int -> t -> [t]
(***) 0 x = []
(***) n x = x : (***) (n-1) x
--------------------------------------------------------------------------------
-- | Add up the elements of a list
--
-- >>> listAdd [1,2,3,4]
-- 10
listAdd :: [Int] -> Int
listAdd [] = 0
listAdd (h:t) = h + listAdd t
--------------------------------------------------------------------------------
-- | Generate the values between lo and hi
--
-- >>> range 0 5
-- [0, 1, 2, 3, 4]
range :: Int -> Int -> [Int]
range lo hi = ite (lo >= hi) [] (lo : rest)
where
lo' = lo + 1
rest = range lo' hi
--------------------------------------------------------------------------------
-- | An If-Then-Else function (!)
-- >>> ifThenElse (1 < 2) "cat" "dog"
-- "cat"
--
-- >>> ifThenElse (10 < 2) "cat" "dog"
-- "dog"
ite :: Bool -> t -> t -> t
ite True x _ = x
ite False _ x = x
{-
function range(lo, hi){
return ite(lo >= hi, [], cons(lo, range(lo+1, hi)));
}
function ite(cond, x, y) {
return cond ? x : y;
}
function cons(x, xs) {
var ys = xs;
ys.push(x);
return ys;
}
-}
--------------------------------------------------------------------------------
-- | Rewrite @range@ , @clone@ using @ite@
range' = undefined
clone' = undefined
--------------------------------------------------------------------------------
-- | Can we write such an @ite@ in JavaScript ?
{-
function ite(b, e1, e2){
return b ? e1 : e2;
}
function clone(n, x){
ite(n > 0, tail.push(x), new Array());
}
-}
--------------------------------------------------------------------------------
-- | Appending two lists
-- >>> append [1,2,3] [4,5,6]
-- [1,2,3,4,5,6]
append :: [a] -> [a] -> [a]
append = undefined
type Pos = Double
type Radius = Double
data CircleT = Circle Pos Pos Radius
deriving (Eq, Show)
type Size = Double
data SquareT = Square Pos Pos Size
deriving (Eq, Show)
data ShapeT = C CircleT
| S SquareT deriving (Eq, Show)
area :: ShapeT -> Double
area (C c) = areaCircle c
area (S s) = areaSquare s
areaSquare :: SquareT -> Double
areaSquare (Square _ _ s) = s * s
areaCircle :: CircleT -> Double
areaCircle (Circle _ _ r) = pi * r * r
--------------------------------------------------------------------------------
-- | Creating Types ------------------------------------------------------------
--------------------------------------------------------------------------------
data Shape = C XPos YPos Radius
| S XPos YPos Side
type XPos = Double
type YPos = Double
type Side = Double
type Radius = Double
area :: Shape -> Double
area (S _ _ side) = side * side
area (C _ _ radius) = pi * radius * radius
-- data Circle = C (Double, Double, Double)
-- C :: (Double, Double, Double) -> Circle
-- radius (C (_,_,r)) = r
-- C (0,1,55)
--
--
--
--
--
-- data Circle = C Double Double Double
-- data Square = S Double Double Double
-- areaCircle (C x y r) = pi * r * r
-- areaSquare (S x y d) = d * d
-- data [a] = []
-- | (:) a [a]
-- data Circle = Kanye Double Double Double
-- Circle :: Double -> Double -> Double -> Circle
-- data Shape = Circle Double Double Double
-- | Square Double Double Double
-- | Poly [(Double, Double)]
-- deriving (Show)
{-
area (Circle _ _ r) = pi * r * r
area (Square _ _ d) = d * d
-- area (Poly ps) = areaPoly ps
areaPoly (p1:p2:p3:rest) = areaTriangle p1 p2 p3 + areaPoly (p1:p3:rest)
areaPoly _ = 0
areaTriangle = undefined -- fill in as exercise
c0 = Circle 0 0 2.3
c1 = Circle 0 1 1.31
s0 = Square 12 1 12312
p0 = Poly [(0,0), (4,4), (92, 92)]
-}
--------------------------------------------------------------------------------
-- | IO
--------------------------------------------------------------------------------
main = putStrLn "Hello World!"
-- main = do putStrLn "What is your name ?"
-- n <- getLine
-- putStrLn ("Happy New Year " ++ n)
-- | Lets break it down. These just *actions*, not executed.
act1 = putStrLn "This is a string on a line"
act2 = putStrLn "This is another string on a line"
act3 = putStrLn "This is the last string i promise you"
-- | acts is just a LIST-OF actions (which is NOT an action)
acts = [act1, act2, act3]
-- | bigAct is the result of COMPOSING the actions
bigAct = do act1
act2
act3
act :: IO ()
act = do putStrLn "Hey? "
name <- getLine
putStrLn ("Hello " ++ name)