-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathWeek02Problems.hs
199 lines (150 loc) · 5.9 KB
/
Week02Problems.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
{-# OPTIONS_GHC -fwarn-incomplete-patterns #-}
module Week02Problems where
import Week02
{------------------------------------------------------------------------------}
{- TUTORIAL QUESTIONS -}
{------------------------------------------------------------------------------}
{- In the questions below, replace 'undefined' with your answers. Use
GHCi to test them.-}
{- 1. Write a function that counts the number of occurrences of an
element in list: -}
popCount :: Eq a => a -> [a] -> Int
popCount = undefined
{- (popCount is short for "population count"). Examples:
popCount 2 [1,2,5,2,7,2,9] == 3
popCount 9 [1,2,5,2,7,2,9] == 1
popCount 0 [1,2,5,2,7,2,9] == 0
-}
{- 2. Write a version of 'insert' that only inserts into a sorted list
if the element is not already there. Examples:
insertNoDup 2 [1,3,4] == [1,2,3,4]
insertNoDup 2 [1,2,3,4] == [1,2,3,4]
-}
insertNoDup :: Ord a => a -> [a] -> [a]
insertNoDup = undefined
{- 3. Write a version of 'remove' that removes all copies of an element
from a sorted list, not just the first one. Examples:
removeAll 2 [1,2,2,3] == [1,3]
removeAll 2 [1,3] == [1,3]
-}
removeAll :: Ord a => a -> [a] -> [a]
removeAll = undefined
{- 4. Rewrite 'treeFind' and 'treeInsert' to use 'compare' and 'case'
expressions. -}
treeFind2 :: Ord k => k -> KV k v -> Maybe v
treeFind2 = undefined
treeInsert2 :: Ord k => k -> v -> KV k v -> KV k v
treeInsert2 = undefined
{- 5. MergeSort is another sorting algorithm that works in the following
way:
- If the list to be sorted is zero length, then it is already
sorted.
- If the list to be sorted has one element, then it is already
sorted.
- Otherwise, split the list into two, one with the even elements
and one with the odd elements. Sort the two lists by calling
'mergeSort' recursively. Then merge the two lists together
maintaining the ordering.
Write this function in three parts: -}
{- 'split' splits the input into two lists: one with the odd numbered
elements and one with the even numbered elements. For example:
> split [45,12,89,29,93]
([45,89,93],[12,29])
HINT: you can pattern match on multiple elements at the head of
a list with 'x1:x2:xs', and you can use the '(odds,evens) = ...'
syntax in a 'where' clause. -}
split :: [a] -> ([a], [a])
split = undefined
{- 'merge' merges two sorted lists into one sorted list. Examples:
merge [1,3,5] [2,4,6] = [1,2,3,4,5,6]
merge [1,3,5] [7,9,11] = [1,3,5,7,9,11]
-}
merge :: Ord a => [a] -> [a] -> [a]
merge = undefined
{- 'mergeSort' uses 'split' and 'merge' to implement the merge sort
algorithm described above. -}
mergeSort :: Ord a => [a] -> [a]
mergeSort = undefined
{- 6. Write another version of 'makeChange' that returns all the
possible ways of making change as a list: -}
makeChangeAll :: [Coin] -> [Coin] -> Int -> [[Coin]]
makeChangeAll = undefined
{- HINT: you don't need a case expression, just a way of appending two
lists of possibilities. -}
{- 7. This question involves converting between two datatypes. A 'Row'
is a list of strings, such as you might find in a database: -}
-- | A row is a list of strings, one for each field. For example:
--
-- > ["Mount Snowden", "Wales"]
type Row = [String]
{- Note that the names of the fields, which might be 'Mountain' and
'Country' here, are implicit in this representation.
The second type is a record, which is a list of pairs of field
names with their data: -}
-- | A record is a list of fieldname / value pairs. For example:
--
-- > [("Mountain", "Mont Blanc"), ("Country", "France")]
type Record = [(String,String)]
{- Implement the following functions on rows and records: -}
-- | Look up a field in a record, returning @Nothing@ if the field is
-- not in the record. For example,
-- > lookupField "a" [("a","1"),("b","2")]
-- returns @Just "1"@, but
-- > lookupField "c" [("a","1"),("b","3")]
-- returns @Nothing@.
lookupField :: String -> Record -> Maybe String
lookupField fieldname record =
error "lookupField: not implemented"
-- | Given a header listing field names, like:
--
-- > ["Mountain", "Country"]
--
-- and a row like:
--
-- > ["Ben Nevis", "Scotland"]
--
-- turn it into a record like:
--
-- > [("Mountain", "Ben Nevis"), ("Country", "Scotland")]
--
-- If the number of field names in the header does not match the
-- number of fields in the row, an @Nothing@ should be returned.
rowToRecord :: [String] -> Row -> Maybe Record
rowToRecord header row =
error "rowToRecord: not implemented"
-- | Given a header listing field names, and a list of rows, converts
-- each row into a record. See 'rowToRecord' for how individual rows
-- are converted to records.
rowsToRecords :: [String] -> [Row] -> Maybe [Record]
rowsToRecords header rows =
error "rowsToRecord: not implemented"
-- | Given a header listing field names, like:
--
-- > ["Mountain", "Country"]
--
-- and a record like:
--
-- > [("Mountain", "Ben Nevis"), ("Country", "Scotland")]
--
-- turn it into a row like:
--
-- > ["Ben Nevis", "Scotland"]
--
-- It does not matter what order the fields in the record are in, so the
-- record:
--
-- > [("Country", "Scotland"), ("Mountain", "Ben Nevis")]
--
-- should result in the same row.
--
-- This function returns an @Nothing@ if any of the field names listed in
-- the header are not in the record.
recordToRow :: [String] -> Record -> Maybe Row
recordToRow header record =
error "recordToRow: not implemented"
-- | Given a header listing field names, and a list of records,
-- converts each record into a row. See 'recordToRow' for how
-- individual records are converted to rows.
recordsToRows :: [String] -> [Record] -> Maybe [Row]
recordsToRows header records =
error "recordsToRows: not implemented"