-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercises.hs
204 lines (151 loc) · 4.09 KB
/
exercises.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
-- Lab 5 --
data BinTree t = Empty | Root t (BinTree t) (BinTree t)
deriving (Eq, Ord, Show)
leaf x = Root x Empty Empty
-- Question 1
addNode :: Ord a => a -> BinTree a -> BinTree a
addNode x Empty = leaf x
addNode x (Root y l r)
| x < y = Root y (addNode x l) r
| otherwise = Root y l (addNode x r)
-- Question 2 didn't get myself but nearly
makeTree :: Ord a => [a] -> BinTree a
makeTree [] = Empty
makeTree [x] = leaf x
makeTree (x:xs) = addNode x (makeTree xs)
-- Question 3 again nearly got but not good enough
inorder :: BinTree a -> [a]
inorder Empty = []
inorder (Root x l r) = inorder l ++ [x] ++ inorder r
-- Question 4
mpsort :: Ord a => [a] -> [a]
mpsort [] = []
mpsort x = inorder (makeTree x)
-- Question 5
reverseorder :: BinTree a -> [a]
reverseorder Empty = []
reverseorder (Root x l r) = reverseorder r ++ [x] ++ reverseorder l
rmpsort :: Ord a => [a] -> [a]
rmpsort [] = []
rmpsort x = reverseorder (makeTree x)
--hosort :: Ord a => (a -> a -> Bool) -> [a] -> [a]
--hosort _ [] = []
--hosort fn x = mpsort x
----hosort (>) x = rmpsort x
myList = [4,3,1,7,5]
myTree1 = Root 5 (Root 1 (Empty) (leaf 3)) (leaf 7)
myTree2 = addNode 6 myTree1
myTree3 = makeTree myList
myTree4 = addNode 6 myTree3
myTree5 = inorder myTree1
myTree6 = reverseorder myTree1
--myTree7 = hosort < [4,3,1,7,5]
--myTree8 = hosort > myList
-- Lab 4 --
-- Question 1
myAppend :: [a] -> [a] -> [a]
myAppend [] x = x
myAppend (y:ys) x = y:(myAppend ys x)
myHead :: [a] -> a
myHead [] = error "List is empty"
myHead (x:xs) = x
myLast :: [a] -> a
myLast [] = error "List is empty"
myLast [x] = x
myLast (x:xs) = myLast xs
myTail :: [a] -> [a]
myTail [] = error "List is empty"
myTail (x:xs) = xs
myInit :: [a] -> [a]
myInit [] = error "List is empty"
myInit [x] = []
myInit (x:xs) = x:(myInit xs)
myLength :: [a] -> Int
myLength [] = error "List is empty"
myLength [x] = 1
myLength (x:xs) = 1 + myLength xs
myReverse :: [a] -> [a]
myReverse [] = error "List is empty"
myReverse [x] = [x]
myReverse (x:xs) = (myReverse xs) ++ [x]
myConcat :: [[a]] -> [a]
myConcat [] = []
myConcat (x:xs) = x ++ (myConcat xs)
mySum :: Num a => [a] -> a
mySum [] = 0
mySum (x:xs) = x + (mySum xs)
myProduct :: Num a => [a] -> a
myProduct [x] = x
myProduct (x:xs) = x * (myProduct xs)
myMaximum :: Ord a => [a] -> a
myMaximum [] = error "List is empty"
myMaximum [x] = x
myMaximum (x:xs)
| x > myMaximum xs = x
| otherwise = myMaximum xs
myMinimum :: Ord a => [a] -> a
myMinimum [] = error "List is empty"
myMinimum [x] = x
myMinimum (x:xs)
| x < (myMinimum xs) = x
| otherwise = myMinimum xs
myElem :: Eq a => a -> [a] -> Bool
myElem x [] = False
myElem x (z:zs)
| x == z = True
| otherwise = myElem x zs
myDelete :: Eq a => a -> [a] -> [a]
myDelete x [] = []
myDelete x (z:zs)
| x == z = zs
| otherwise = z:(myDelete x zs)
-- Question 2 not my work
myUnion :: (Eq a) => [a] -> [a] -> [a]
myUnion xs [] = xs
myUnion xs (y:ys)
| (myElem y xs) || (myElem y ys) = myUnion xs ys
| otherwise = myUnion (myAppend xs [y]) ys
myIntersect :: (Eq a) => [a] -> [a] -> [a]
myIntersect [] _ = []
myIntersect (x:xs) ys
| myElem x ys = x:(myIntersect xs ys)
| otherwise = myIntersect xs ys
-- Lab 3 --
-- Question 1
isPalindrome :: Eq a => [a] -> Bool
isPalindrome xs = xs == reverse xs
-- Question 2
shortest [x] = x
shortest (x:xs)
| length x < length (shortest xs) = x
| otherwise = shortest xs
-- Question 3
type Poly = [Float]
sumPolys :: Poly -> Poly -> Poly
sumPolys [] p = p
sumPolys p [] = p
sumPolys (x:xs) (y:ys) = (x+y):(sumPolys xs ys)
-- Question 4
evalPoly :: Float-> Poly -> Float
evalPoly _ [z] = z
evalPoly x (z:zs) = z + (x * (evalPoly x zs))
-- Lab 2 --
-- Questions 1 and 3
triangleArea :: Float -> Float -> Float -> Float
triangleArea a b c
| a + b < c = error "Not a triangle!"
| b + c < a = error "Not a triangle!"
| a + c < b = error "Not a triangle!"
| otherwise =
let s = (a + b + c) / 2
in sqrt(s*(s - a)*(s - b)*(s - c))
-- Question 2
isSum :: Int -> Int -> Int -> Bool
isSum x y z
| x + y == z = True
| x + z == y = True
| z + y == x = True
| otherwise = False
-- Lab 1 --
diff :: Int -> Int -> Int
diff x y = abs (x-y)