/
fp2.scala
182 lines (170 loc) · 7.03 KB
/
fp2.scala
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
// GENERATED
/* INSTRUCTIONS
*
* Complete the exercises below. For each "EXERCISE" comment, add
* code immediately below the comment.
*
* Please see README.md for instructions, including compilation and testing.
*
* GRADING
*
* 1. Submissions MUST compile using SBT with UNCHANGED configuration and tests with no
* compilation errors. Submissions with compilation errors will receive 0 points.
* Note that refactoring the code will cause the tests to fail.
*
* 2. You MUST NOT edit the SBT configuration and tests. Altering it in your submission will
* result in 0 points for this assignment.
*
* 3. You MUST NOT use while loops or (re)assignment to variables (you can use "val" declarations,
* but not "var" declarations). You must use recursion instead.
*
* 4. You may declare auxiliary functions if you like.
*
* SUBMISSION
*
* 1. Submit this file on D2L before the deadline.
*
* 2. Late submissions will not be permitted because solutions will be discussed in class.
*
*/
object fp2 {
// EXERCISE 1: complete the following recursive definition of a "map" function
// for Scala's builtin List type. You must not use the builtin "map" method.
// Your implementation of "map" MUST be recursive.
def map [A,B] (xs:List[A], f:A=>B) : List[B] = {
// TODO: Provide definition here.
xs match{
case Nil => Nil
case y::ys => f(y)::map(ys,f)
}
}
// EXERCISE 2: complete the following recursive definition of a "filter" function
// for Scala's builtin List type. You must not use the builtin "filter" method.
// Your implementation of "filter" MUST be recursive.
def filter [A] (xs:List[A], f:A=>Boolean) : List[A] = {
// TODO: Provide definition here.
xs match{
case Nil => Nil
case y::ys => {
if (f(y))
y::filter(ys,f)
else
filter(ys,f)
}
}
}
// EXERCISE 3: complete the following recursive definition of an "append" function
// for Scala's builtin List type. You must not use the builtin ":::" method.
// Your implementation of "append" MUST be recursive.
// HINT: use "::" in the body of the cons-cell case.
def append [A] (xs:List[A], ys:List[A]) : List[A] = {
// TODO: Provide definition here.
xs match{
case List() => ys
case x:: tail => x::append(xs.tail,ys)
}
}
// EXERCISE 4: complete the following recursive definition of a "flatten" function
// for Scala's builtin List type. You must not use the builtin "flatten" method.
// Your implementation of "flatten" MUST be recursive.
// HINT: use either ":::" or your definition of "append" in the body of the cons-cell case.
// EXAMPLE:
// - flatten (List ((1 to 5).toList, (6 to 10).toList, (11 to 15).toList)) == (1 to 15).toList
def flatten [A] (xss:List[List[A]]) : List[A] = {
// TODO: Provide definition here.
xss match {
case Nil => Nil
case ys::yss => ys:::flatten(yss)
}
}
// EXERCISE 5: complete the following recursive definition of a "foldLeft" function
// for Scala's builtin list type. You must not use the builtin "foldLeft" method.
// Your implementation of "foldLeft" MUST be recursive.
// HINT: foldLeft ( Nil, e, f) == e
// foldLeft (y::ys, e, f) == foldLeft (ys, f (e, y), f)
// This is tail recursive
def foldLeft [A,B] (xs:List[A], e:B, f:(B,A)=>B) : B = {
// TODO: Provide definition here.
xs match {
case Nil => e
case y::ys => foldLeft(ys,f(e,y),f)
}
}
// EXERCISE 6: complete the following recursive definition of a "foldRight" function
// for Scala's builtin list type. You must not use the builtin "foldRight" method.
// Your implementation of "foldRight" MUST be recursive.
// HINT: foldRight ( Nil, e, f) == e
// foldRight (y::ys, e, f) == f (y, foldRight (ys, e, f))
// This is not tail recursive
def foldRight [A,B] (xs:List[A], e:B, f:(A,B)=>B) : B = {
// TODO: Provide definition here.
xs match {
case Nil => e
case y::ys => f(y, foldRight(ys,e,f))
}
}
// EXERCISE 7: complete the following definition of a "joinTerminateRight" function
// to take a list of strings "xs" and concatenate all strings using a string "term"
// as a terminator (not delimiter) between strings. You MUST use your foldRight defined above.
// EXAMPLES:
// - joinTerminateRight (Nil, ";") == ""
// - joinTerminateRight (List ("a"), ";") == "a;"
// - joinTerminateRight (List ("a","b","c","d"), ";") == "a;b;c;d;"
def joinTerminateRight (xs : List[String], term : String) : String = {
// TODO: Provide definition here.
def f(acc:String, s:String) : String = term + acc+ s
xs match{
case Nil => ""
case y::ys =>y + foldRight(ys,term,f)
}
}
// EXERCISE 8: complete the following definition of a "joinTerminateLeft" function
// to take a list of strings "xs" and concatenate all strings using a string "term"
// as a terminator (not delimiter) between strings. You MUST use your foldLeft defined above.
// EXAMPLES:
// - joinTerminateLeft (Nil, ";") == ""
// - joinTerminateLeft (List ("a"), ";") == "a;"
// - joinTerminateLeft (List ("a","b","c","d"), ";") == "a;b;c;d;"
def joinTerminateLeft (xs : List[String], term : String) : String = {
// TODO: Provide definition here.
def f(acc:String, s:String):String = acc + s + term
xs match{
case Nil => ""
case y::ys => foldLeft(ys,f("",y),f)
}
}
// EXERCISE 9: complete the following recursive definition of a "firstNumGreaterThan" function
// to find the first number greater than or equal to "a" in a list of integers "xs".
// If the list is empty or there is no number greater than or equal to "a",
// throw a java.util.NoSuchElementException (with no argument).
// Your implementation of "firstNumGreaterThan" MUST be recursive.
// EXAMPLES:
// - firstNumGreaterThan (5, List (4, 6, 8, 5)) == 6
def firstNumGreaterThan (a : Int, xs : List[Int]) : Int = {
// TODO: Provide definition here.
xs match{
case Nil => throw new RuntimeException
case y::ys => if(y>=a) y else firstNumGreaterThan(a,ys)
}
}
// EXERCISE 10: complete the following recursive definition of a "firstIndexNumGreaterThan" function
// to find the index (position) of the first number greater than or equal to "a" in a list of integers "xs".
// If the list is empty or there is no number greater than or equal to "a", throw a
// java.util.NoSuchElementException (with no argument).
// The first index should be zero (not one).
// Your implementation of "firstIndexNumGreaterThan" MUST be recursive.
// EXAMPLES:
// - firstIndexNumGreaterThan (5, List (4, 6, 8, 5)) == 1
// HINT: this is a bit easier to write if you use an auxiliary function.
def firstIndexNumGreaterThan (a : Int, xs : List[Int]) : Int = {
// TODO: Provide definition here.
def firstIndexAux(a: Int, xs: List[Int], i: Int): Int={
xs match {
case Nil => throw new RuntimeException
case y::ys => if (y>=a) i
else firstIndexAux(a,ys,i+1)
}
}
firstIndexAux(a,xs,0)
}
}