forked from markhibberd/introduction-to-fp-in-scala
-
Notifications
You must be signed in to change notification settings - Fork 5
/
List.scala
160 lines (148 loc) · 3.93 KB
/
List.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
package intro
object Lists {
/*
*
* The following examples are all based upon the 'List'
* data structure. We will be using the List implementation
* from the standard library.
*
* In scala this data structure looks like this:
*
* {{{
* sealed trait List[+A]
* case object Nil extends List[Nothing]
* case class ::[A](h: A, t: List[A]) extends List[A]
* }}}
*
* We call this a "sum-type", where "List" is a type
* constructor that has two data constructors, "Nil",
* and "::" (pronounced ~cons~). We can declare values
* of type List using either the data constructors or
* via the convenience function `List`.
*
* {{{
* val xs = "goodbye" :: "cruel" :: "world" :: Nil
* val ys = List("we", "have", "the", "technology")
* }}}
*
* Lists can be worked with via pattern matching or via
* the standard library methods foldRight & foldLeft.
* These are defined as:
*
* {{{
* List[A]#foldRight[B](z: B)(f: (A, B) => B)
* List[A]#foldLeft[B](z: B)(f: (B, A) => B)
* }}}
*
*/
/*
* Exercise 1:
*
* Implement length using pattern matching.
*
* scala> Lists.length(List(1, 2, 3, 4))
* resX: Int = 4
*/
def length[A](xs: List[A]): Int =
???
/*
* Exercise 2:
*
* Implement length using foldRight.
*
* scala> Lists.lengthX(List(1, 2, 3, 4))
* resX: Int = 4
*/
def lengthX[A](xs: List[A]): Int =
???
/*
* Exercise 3:
*
* Append two lists to produce a new list.
*
* scala> Lists.append(List(1, 2, 3, 4), List(5, 6, 7, 8))
* resX: List[Int] = List(1, 2, 3, 4, 5, 6, 7, 8)
*/
def append[A](x: List[A], y: List[A]): List[A] =
???
/*
* Exercise 4:
*
* Map the given function across each element of the list.
*
* scala> Lists.map(List(1, 2, 3, 4))(x => x + 1)
* resX: List[Int] = List(2, 3, 4, 5)
*
* ~~~ Syntax hint: type annotations
*
* (Nil : List[A]) // Nil _is of type_ List[A]
*
* Type annotations are required when scala can
* not infer what you mean.
*/
def map[A, B](xs: List[A])(f: A => B): List[B] =
???
/*
* Exercise 5:
*
* Return elements satisfying the given predicate.
*
* scala> Lists.filter(List(1, 2, 3, 4))(i => i < 3)
* resX: List[Int] = List(1, 2)
*/
def filter[A](xs: List[A])(p: A => Boolean): List[A] =
???
/*
* Exercise 6:
*
* Reverse a list to produce a new list.
*
* scala> Lists.reverse(List( 1, 2, 3, 4))
* resX: List[Int] = List(4, 3, 2, 1)
*
* ~~~ Syntax hint: type annotations
*
* (Nil : List[A]) // Nil _is of type_ List[A]
*
* Type annotations are required when scala can
* not infer what you mean.
*/
def reverse[A](xs: List[A]): List[A] =
???
/*
* *Challenge* Exercise 7:
*
* Sequence a list of Option into an Option of Lists by producing
* Some of a list of all the values or returning None on the first
* None case.
*
* scala> Lists.sequence(List[Option[Int]](Some(1), Some(2), Some(3)))
* resX: Option[List[Int]] = Some(List(1, 2, 3))
*
* scala> Lists.sequence(List[Option[Int]](Some(1), None, Some(3)))
* resX: Option[List[Int]] = None
*/
def sequence[A](xs: List[Option[A]]): Option[List[A]] =
???
/*
* *Challenge* Exercise 8:
*
* Return a list of ranges. A range is a pair of values for which each
* intermediate value exists in the list.
*
*
* scala> Lists.ranges(List(1, 2, 3, 4, 7, 8, 9, 10, 30, 40, 41))
* resX: List[(Int, Int)] = List((1, 4), (7, 10), (30, 30), (40, 41))
*
* scala> Lists.ranges(List(1, 2, 3, 4))
* resX: List[(Int, Int)] = List((1, 4))
*
* scala> Lists.ranges(List(1, 2, 4))
* resX: List[(Int, Int)] = List((1, 2), (4, 4))
*
* ~~~ library hint: use can just use List[A]#sorted and/or List[A]#reverse to
* get the list in the correct order. *
*/
def ranges(xs: List[Int]): List[(Int, Int)] =
???
}