-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart_1.hs
236 lines (183 loc) · 4.43 KB
/
part_1.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
-- 1) a)
{-
Example:
f :: (a,b) -> X
f (a,b) = ...
It’s well typed. Subexpresions
•(a,b) :: (a,b)
•a :: a
•b :: b
The pattern covers every possible type
-}
{-
f :: (a,b) -> X
f x = ...
It’s well typed. Subexpresions
•x :: (a,b)
The pattern does not cover every possible type
-}
{-
f :: (a,b) -> X
f (x,y) = ...
It’s well typed. Subexpresions
•(x,y) :: (a,b)
•x :: a
•y :: b
The pattern does not cover every possible type
-}
{-
f :: [(a,b)] -> X
f (a,b) = ...
It’s not well typed.
-}
{-
f :: [(a,b)] -> X
f (x:xs) = ...
It’s well typed. Subexpresions
•(x:xs) :: [(a,b)]
•x :: (a,b)
•xs :: [(a,b)]
The pattern does not cover every possible type
-}
{-
f :: [(a,b)] -> X
f ((x,y):((a,b):xs)) = ...
It’s well typed. Subexpresions
•((x,y):((a,b):xs)) :: [(a,b)]
•(x, y) :: (a,b)
•x :: a
•y :: b
•((a,b):xs) :: [(a,b)]
•(a,b) :: (a,b)
•a :: a
•b :: b
•xs :: [(a,b)]
The pattern covers every possible type
-}
{-
f :: [(Int,a)] -> X
f [(0,a)] = ...
It’s well typed. Subexpresions
•[(0,a)] :: [(Int,a)]
•0 :: Int
•a :: a
The pattern covers every possible type
-}
{-
f :: [(Int,a)] -> X
f ((x,1):xs) = ...
It’s well typed. Subexpresions
•((x,1):xs) :: [(Int,a)]
•(x,1) :: (Int,a)
•x :: Int
•1 :: Int
•a :: Int
•xs :: [(Int,a)]
The pattern covers every possible type
-}
{-
f :: [(Int,a)] -> X
f ((1,x):xs) = ...
It’s well typed. Subexpresions
•((1,x):xs) :: [(Int,a)]
•(1,x) :: (Int,a)
•x :: a
•1 :: Int
•xs :: [(Int,a)]
The pattern covers every possible type
-}
{-
f :: (Int -> Int) -> Int -> X
f a b = ...
It’s well typed. Subexpresions
•a :: Int -> Int
•b :: Int
The pattern does not cover every possible type
-}
{-
f :: (Int -> Int) -> Int -> X
f a 3 = ...
It’s well typed. Subexpresions
•a :: Int -> Int
•3 :: Int
The pattern does not cover every possible type
-}
{-
f :: (Int -> Int) -> Int -> X
f 0 1 2 = ...
It’s no well typed
-}
{-
f :: Int -> Int -> Int -> X
f 0 1 2 = ...
It’s well typed. Subexpresions
•0 :: Int
•1 :: Int
•2 :: Int
The pattern covers every possible type
-}
{-
f :: Int -> Int -> Int -> X
f 0 g = ...
It’s not well typed
-}
{-
f :: a -> (a -> a) -> X
f 0 g = ...
It’s well typed. Subexpresions
•0 :: a
•g :: a -> a
The pattern does not cover every possible type
-}
-- 1) b)
snd' :: (a,b) -> b
snd' (_,y) = y
-- There exists another different definition? No
errorFunc :: a -> b
errorFunc x = error "error"
-- There exists another different definition? yes
-- f :: (a,b) -> c non definable
apply_function :: (a -> b) -> a -> b
apply_function f x = f x
-- There exists another different definition? no
-- f :: (a -> b) -> a -> c non definable
list_funct :: (a -> b) -> [a] -> [b]
list_funct f x = map f x
-- There exists another different definition? no
-- (-.)' :: (a -> b) -> (b -> c) -> a -> c
-- (-.)' = (<&>)
-- There exists another different definition? no
transitivity :: (a -> b) -> (b -> c) -> [a] -> [c]
transitivity f g x = map (g . f) x
-- There exists another different definition? no
-- 2)
optimizeList :: Eq a => [ a ] -> [ (Int, a) ]
optimizeList [] = []
optimizeList (x:xs) = (1 + length (takeWhile (==x) xs), x) : optimizeList (dropWhile (==x) xs)
-- 3)
data Tree = Leaf Int | Node Tree Tree
-- 3) a)
height :: Tree -> Int
height (Leaf _) = 0
height (Node l r) = 1 + max (height l) (height r)
balanced :: Tree -> Bool
balanced (Leaf _) = True
balanced (Node l r) = abs (height l - height r) <= 1 && balanced l && balanced r
--3) b)
balance :: [Int] -> Tree
balance [x] = Leaf x
balance xs = Node (balance (take (length xs `div` 2) xs)) (balance (drop (length xs `div` 2) xs))
-- 4
infLeftTree = Node (goLeft infLeftTree) (Leaf 1)
goLeft :: Tree -> Tree
goLeft (Leaf _) = error "leaf"
goLeft (Node t _) = t
flatten :: Tree -> [Int]
flatten (Leaf i) = [i]
flatten (Node t d) = flatten d ++ flatten t
-- is it even possible? Yes, it is. Due to Haskell Lazy Evaluation, expressions are not evaluated when they are bound
-- to variables but their evaluation is deferred until their results are needed by other computations. In consequence,
-- arguments are not evaluated before they are passed to a function, but only when their values are actually used.
-- If yes, why does it work head (flatten infLeftTree)? Thats because the head function just takes the first element of the list,
-- ignoring the rest of the elements. So, the evaluation of the rest of the list is not needed. Therefore, those infinite elements
-- are not evaluated.