67
|
1 |
// Scala Lecture 3
|
|
2 |
//=================
|
|
3 |
|
|
4 |
|
|
5 |
// One of only two places where I conceded to mutable
|
|
6 |
// data structures: The following function generates
|
|
7 |
// new labels
|
|
8 |
|
|
9 |
var counter = -1
|
|
10 |
|
|
11 |
def fresh(x: String) = {
|
|
12 |
counter += 1
|
|
13 |
x ++ "_" ++ counter.toString()
|
|
14 |
}
|
|
15 |
|
|
16 |
fresh("x")
|
|
17 |
fresh("x")
|
|
18 |
|
|
19 |
// this can be avoided, but would have made my code more
|
|
20 |
// complicated
|
|
21 |
|
|
22 |
|
|
23 |
// Tail recursion
|
|
24 |
//================
|
|
25 |
|
71
|
26 |
def my_contains(elem: Int, lst: List[Int]): Boolean = lst match {
|
67
|
27 |
case Nil => false
|
|
28 |
case x::xs =>
|
|
29 |
if (x == elem) true else my_contains(elem, xs)
|
|
30 |
}
|
|
31 |
|
|
32 |
my_contains(4, List(1,2,3))
|
|
33 |
my_contains(2, List(1,2,3))
|
|
34 |
|
|
35 |
my_contains(1000000, (1 to 1000000).toList)
|
|
36 |
my_contains(1000001, (1 to 1000000).toList)
|
|
37 |
|
|
38 |
|
71
|
39 |
//factorial V0.1
|
72
|
40 |
import scala.annotation.tailrec
|
|
41 |
|
67
|
42 |
|
|
43 |
def fact(n: Long): Long =
|
|
44 |
if (n == 0) 1 else n * fact(n - 1)
|
|
45 |
|
71
|
46 |
fact(10000) // produces a stackoverflow
|
67
|
47 |
|
72
|
48 |
@tailrec
|
67
|
49 |
def factT(n: BigInt, acc: BigInt): BigInt =
|
|
50 |
if (n == 0) acc else factT(n - 1, n * acc)
|
|
51 |
|
|
52 |
|
72
|
53 |
println(factT(10000, 1))
|
67
|
54 |
|
71
|
55 |
// the functions my_contains and factT are tail-recursive
|
67
|
56 |
// you can check this with
|
|
57 |
|
|
58 |
import scala.annotation.tailrec
|
|
59 |
|
|
60 |
// and the annotation @tailrec
|
|
61 |
|
71
|
62 |
// for tail-recursive functions the scala compiler
|
|
63 |
// generates loop-like code, which does not need
|
67
|
64 |
// to allocate stack-space in each recursive
|
|
65 |
// call; scala can do this only for tail-recursive
|
|
66 |
// functions
|
|
67 |
|
|
68 |
// consider the following "stupid" version of the
|
71
|
69 |
// coin exchange problem: given some coins and a
|
|
70 |
// total, what is the change can you get?
|
53
|
71 |
|
71
|
72 |
val coins = List(4,5,6,8,10,13,19,20,21,24,38,39,40)
|
67
|
73 |
|
|
74 |
def first_positive[B](lst: List[Int], f: Int => Option[B]): Option[B] = lst match {
|
|
75 |
case Nil => None
|
|
76 |
case x::xs =>
|
|
77 |
if (x <= 0) first_positive(xs, f)
|
|
78 |
else {
|
|
79 |
val fx = f(x)
|
|
80 |
if (fx.isDefined) fx else first_positive(xs, f)
|
|
81 |
}
|
|
82 |
}
|
|
83 |
|
|
84 |
|
72
|
85 |
import scala.annotation.tailrec
|
|
86 |
|
67
|
87 |
def search(total: Int, coins: List[Int], cs: List[Int]): Option[List[Int]] = {
|
|
88 |
if (total < cs.sum) None
|
|
89 |
else if (cs.sum == total) Some(cs)
|
|
90 |
else first_positive(coins, (c: Int) => search(total, coins, c::cs))
|
|
91 |
}
|
|
92 |
|
|
93 |
search(11, coins, Nil)
|
|
94 |
search(111, coins, Nil)
|
|
95 |
search(111111, coins, Nil)
|
53
|
96 |
|
67
|
97 |
val junk_coins = List(4,-2,5,6,8,0,10,13,19,20,-3,21,24,38,39, 40)
|
|
98 |
search(11, junk_coins, Nil)
|
|
99 |
search(111, junk_coins, Nil)
|
|
100 |
|
|
101 |
|
|
102 |
import scala.annotation.tailrec
|
|
103 |
|
|
104 |
@tailrec
|
72
|
105 |
def searchT(total: Int, coins: List[Int],
|
|
106 |
acc_cs: List[List[Int]]): Option[List[Int]] = acc_cs match {
|
67
|
107 |
case Nil => None
|
|
108 |
case x::xs =>
|
71
|
109 |
if (total < x.sum) searchT(total, coins, xs)
|
67
|
110 |
else if (x.sum == total) Some(x)
|
71
|
111 |
else searchT(total, coins, coins.filter(_ > 0).map(_::x) ::: xs)
|
67
|
112 |
}
|
|
113 |
|
|
114 |
val start_acc = coins.filter(_ > 0).map(List(_))
|
71
|
115 |
searchT(11, junk_coins, start_acc)
|
|
116 |
searchT(111, junk_coins, start_acc)
|
|
117 |
searchT(111111, junk_coins, start_acc)
|
67
|
118 |
|
|
119 |
// moral: whenever a recursive function is resource-critical
|
|
120 |
// (i.e. works on large recursion depth), then you need to
|
|
121 |
// write it in tail-recursive fashion
|
|
122 |
|
|
123 |
|
71
|
124 |
// Polymorphic Types
|
|
125 |
//===================
|
|
126 |
|
72
|
127 |
// You do not want to write functions like contains, first
|
71
|
128 |
// and so on for every type of lists.
|
|
129 |
|
67
|
130 |
|
72
|
131 |
def length_string_list(lst: List[String]): Int = lst match {
|
67
|
132 |
case Nil => 0
|
72
|
133 |
case x::xs => 1 + length_string_list(xs)
|
67
|
134 |
}
|
|
135 |
|
72
|
136 |
length_string_list(List("1", "2", "3", "4"))
|
67
|
137 |
|
|
138 |
|
|
139 |
def length[A](lst: List[A]): Int = lst match {
|
|
140 |
case Nil => 0
|
|
141 |
case x::xs => 1 + length(xs)
|
|
142 |
}
|
|
143 |
|
53
|
144 |
|
67
|
145 |
def map_int_list(lst: List[Int], f: Int => Int): List[Int] = lst match {
|
|
146 |
case Nil => Nil
|
|
147 |
case x::xs => f(x)::map_int_list(xs, f)
|
|
148 |
}
|
|
149 |
|
|
150 |
map_int_list(List(1, 2, 3, 4), square)
|
|
151 |
|
|
152 |
|
|
153 |
// Remember?
|
|
154 |
def first[A, B](xs: List[A], f: A => Option[B]): Option[B] = ...
|
|
155 |
|
|
156 |
|
|
157 |
// polymorphic classes
|
|
158 |
//(trees with some content)
|
|
159 |
|
|
160 |
abstract class Tree[+A]
|
|
161 |
case class Node[A](elem: A, left: Tree[A], right: Tree[A]) extends Tree[A]
|
|
162 |
case object Leaf extends Tree[Nothing]
|
|
163 |
|
72
|
164 |
val t0 = Node('4', Node('2', Leaf, Leaf), Node('7', Leaf, Leaf))
|
|
165 |
|
67
|
166 |
def insert[A](tr: Tree[A], n: A): Tree[A] = tr match {
|
|
167 |
case Leaf => Node(n, Leaf, Leaf)
|
|
168 |
case Node(m, left, right) =>
|
|
169 |
if (n == m) Node(m, left, right)
|
|
170 |
else if (n < m) Node(m, insert(left, n), right)
|
|
171 |
else Node(m, left, insert(right, n))
|
|
172 |
}
|
|
173 |
|
|
174 |
|
|
175 |
// the A-type needs to be ordered
|
|
176 |
|
|
177 |
abstract class Tree[+A <% Ordered[A]]
|
72
|
178 |
case class Node[A <% Ordered[A]](elem: A, left: Tree[A],
|
|
179 |
right: Tree[A]) extends Tree[A]
|
67
|
180 |
case object Leaf extends Tree[Nothing]
|
|
181 |
|
|
182 |
|
|
183 |
def insert[A <% Ordered[A]](tr: Tree[A], n: A): Tree[A] = tr match {
|
|
184 |
case Leaf => Node(n, Leaf, Leaf)
|
|
185 |
case Node(m, left, right) =>
|
|
186 |
if (n == m) Node(m, left, right)
|
|
187 |
else if (n < m) Node(m, insert(left, n), right)
|
|
188 |
else Node(m, left, insert(right, n))
|
|
189 |
}
|
|
190 |
|
|
191 |
|
|
192 |
val t1 = Node(4, Node(2, Leaf, Leaf), Node(7, Leaf, Leaf))
|
|
193 |
insert(t1, 3)
|
|
194 |
|
|
195 |
val t2 = Node('b', Node('a', Leaf, Leaf), Node('f', Leaf, Leaf))
|
|
196 |
insert(t2, 'e')
|
53
|
197 |
|
|
198 |
|
|
199 |
|
71
|
200 |
// Regular expressions - the power of DSLs in Scala
|
|
201 |
//==================================================
|
67
|
202 |
|
|
203 |
|
|
204 |
abstract class Rexp
|
|
205 |
case object ZERO extends Rexp
|
|
206 |
case object ONE extends Rexp
|
|
207 |
case class CHAR(c: Char) extends Rexp
|
71
|
208 |
case class ALT(r1: Rexp, r2: Rexp) extends Rexp // alternative r1 + r2
|
72
|
209 |
case class SEQ(r1: Rexp, r2: Rexp) extends Rexp // sequence r1 r2
|
71
|
210 |
case class STAR(r: Rexp) extends Rexp // star r*
|
67
|
211 |
|
|
212 |
|
|
213 |
// (ab)*
|
72
|
214 |
val r0 = STAR(SEQ(CHAR('a'), CHAR('b')))
|
67
|
215 |
|
|
216 |
|
|
217 |
// some convenience for typing in regular expressions
|
|
218 |
import scala.language.implicitConversions
|
|
219 |
import scala.language.reflectiveCalls
|
|
220 |
|
|
221 |
def charlist2rexp(s: List[Char]): Rexp = s match {
|
|
222 |
case Nil => ONE
|
|
223 |
case c::Nil => CHAR(c)
|
|
224 |
case c::s => SEQ(CHAR(c), charlist2rexp(s))
|
|
225 |
}
|
|
226 |
implicit def string2rexp(s: String): Rexp = charlist2rexp(s.toList)
|
|
227 |
|
|
228 |
|
|
229 |
val r1 = STAR("ab")
|
|
230 |
val r2 = STAR("")
|
72
|
231 |
val r3 = STAR(ALT("ab", "baa baa black sheep"))
|
67
|
232 |
|
|
233 |
implicit def RexpOps (r: Rexp) = new {
|
|
234 |
def | (s: Rexp) = ALT(r, s)
|
|
235 |
def % = STAR(r)
|
|
236 |
def ~ (s: Rexp) = SEQ(r, s)
|
|
237 |
}
|
|
238 |
|
|
239 |
implicit def stringOps (s: String) = new {
|
|
240 |
def | (r: Rexp) = ALT(s, r)
|
|
241 |
def | (r: String) = ALT(s, r)
|
|
242 |
def % = STAR(s)
|
|
243 |
def ~ (r: Rexp) = SEQ(s, r)
|
|
244 |
def ~ (r: String) = SEQ(s, r)
|
|
245 |
}
|
|
246 |
|
|
247 |
val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
|
|
248 |
val sign = "+" | "-" | ""
|
|
249 |
val number = sign ~ digit ~ digit.%
|
|
250 |
|
|
251 |
|
|
252 |
|
|
253 |
// Lazyness with style
|
|
254 |
//=====================
|
|
255 |
|
|
256 |
// The concept of lazy evaluation doesn’t really exist in
|
|
257 |
// non-functional languages, but it is pretty easy to grasp.
|
|
258 |
// Consider first
|
|
259 |
|
|
260 |
def square(x: Int) = x * x
|
|
261 |
|
|
262 |
square(42 + 8)
|
|
263 |
|
|
264 |
// this is called strict evaluation
|
|
265 |
|
|
266 |
|
|
267 |
def expensiveOperation(n: BigInt): Boolean = expensiveOperation(n + 1)
|
|
268 |
val a = "foo"
|
72
|
269 |
val b = "bar"
|
67
|
270 |
|
|
271 |
val test = if ((a == b) || expensiveOperation(0)) true else false
|
|
272 |
|
|
273 |
// this is called lazy evaluation
|
|
274 |
// you delay compuation until it is really
|
|
275 |
// needed; once calculated though, does not
|
|
276 |
// need to be re-calculated
|
|
277 |
|
|
278 |
// a useful example is
|
|
279 |
def time_needed[T](i: Int, code: => T) = {
|
|
280 |
val start = System.nanoTime()
|
|
281 |
for (j <- 1 to i) code
|
|
282 |
val end = System.nanoTime()
|
|
283 |
((end - start) / i / 1.0e9) + " secs"
|
|
284 |
}
|
|
285 |
|
|
286 |
|
|
287 |
// streams (I do not care how many)
|
|
288 |
// primes: 2, 3, 5, 7, 9, 11, 13 ....
|
|
289 |
|
|
290 |
def generatePrimes (s: Stream[Int]): Stream[Int] =
|
|
291 |
s.head #:: generatePrimes(s.tail filter (_ % s.head != 0))
|
|
292 |
|
|
293 |
val primes: Stream[Int] = generatePrimes(Stream.from(2))
|
|
294 |
|
73
|
295 |
primes.take(10).toList
|
|
296 |
|
67
|
297 |
primes.filter(_ > 100).take(2000).toList
|
|
298 |
|
|
299 |
time_needed(1, primes.filter(_ > 100).take(2000).toList)
|
|
300 |
time_needed(1, primes.filter(_ > 100).take(2000).toList)
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
// streams are useful for implementing search problems ;o)
|
|
305 |
|
|
306 |
|
|
307 |
|
|
308 |
|
|
309 |
// The End
|
|
310 |
//=========
|
|
311 |
|
|
312 |
// A function should do one thing, and only one thing.
|
|
313 |
|
|
314 |
// Make your variables immutable, unless there's a good
|
|
315 |
// reason not to.
|
|
316 |
|
|
317 |
// You can be productive on Day 1, but the language is deep.
|
|
318 |
|
68
|
319 |
// I like best about Scala that it lets me write
|
67
|
320 |
// concise, readable code
|
68
|
321 |
|