equal
deleted
inserted
replaced
7 |
7 |
8 // - recursion |
8 // - recursion |
9 // - Sudoku |
9 // - Sudoku |
10 // - string interpolations |
10 // - string interpolations |
11 // - Pattern-Matching |
11 // - Pattern-Matching |
|
12 |
|
13 def fact(n: BigInt) : BigInt = { |
|
14 if (n == 0) 1 |
|
15 else n * fact(n - 1) |
|
16 } |
|
17 |
|
18 def fib(n: BigInt) : BigInt = { |
|
19 if (n == 0) 1 |
|
20 else if (n == 1) 1 |
|
21 else fib(n - 1) + fib(n - 2) |
|
22 } |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 def inc(n: Int) : Int = n + 1 |
|
29 |
|
30 for (n <- List(1,2,3,4)) yield inc(n) |
|
31 |
|
32 List().map(inc) |
|
33 |
|
34 |
|
35 |
|
36 my_map(inc, List(1,2,3,4)) |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
12 |
42 |
13 // A Recursive Web Crawler / Email Harvester |
43 // A Recursive Web Crawler / Email Harvester |
14 //=========================================== |
44 //=========================================== |
15 // |
45 // |
16 // the idea is to look for links using the |
46 // the idea is to look for links using the |
77 |
107 |
78 // THE POINT OF THIS CODE IS NOT TO BE SUPER |
108 // THE POINT OF THIS CODE IS NOT TO BE SUPER |
79 // EFFICIENT AND FAST, just explaining exhaustive |
109 // EFFICIENT AND FAST, just explaining exhaustive |
80 // depth-first search |
110 // depth-first search |
81 |
111 |
|
112 //> using dep org.scala-lang.modules::scala-parallel-collections:1.0.4 |
|
113 import scala.collection.parallel.CollectionConverters.* |
|
114 |
|
115 |
|
116 val s1 = "s\n" |
|
117 val s2 = """s\n""" |
82 |
118 |
83 val game0 = """.14.6.3.. |
119 val game0 = """.14.6.3.. |
84 |62...4..9 |
120 |62...4..9 |
85 |.8..5.6.. |
121 |.8..5.6.. |
86 |.6.2....3 |
122 |.6.2....3 |
284 // case pattern2 => expression2 |
320 // case pattern2 => expression2 |
285 // ... |
321 // ... |
286 // case patternN => expressionN |
322 // case patternN => expressionN |
287 // } |
323 // } |
288 |
324 |
|
325 def my_map(f: Int => Int, xs: List[Int]) : List[Int] = |
|
326 xs match { |
|
327 case Nil => Nil |
|
328 case hd::tl => f(hd) :: my_map(f, tl) |
|
329 } |
|
330 |
|
331 my_map(x => x * x, List(1,2,3,4)) |
|
332 |
289 |
333 |
290 // recall |
334 // recall |
291 def len(xs: List[Int]) : Int = { |
335 def len(xs: List[Int]) : Int = { |
292 if (xs == Nil) 0 |
336 if (xs == Nil) 0 |
293 else 1 + len(xs.tail) |
337 else 1 + len(xs.tail) |
348 println(fizz_buzz(n)) |
392 println(fizz_buzz(n)) |
349 |
393 |
350 // more interesting patterns for lists - calculate the deltas between |
394 // more interesting patterns for lists - calculate the deltas between |
351 // elements |
395 // elements |
352 |
396 |
|
397 List(4,3,2,1) -> List(1,1,.. ) |
|
398 |
353 def delta(xs: List[Int]) : List[Int] = xs match { |
399 def delta(xs: List[Int]) : List[Int] = xs match { |
354 case Nil => Nil |
400 case Nil => Nil |
355 case x::Nil => x::Nil |
401 case x::Nil => x::Nil |
356 case x::y::xs => (x - y)::delta(y::xs) |
402 case x::y::xs => (x - y)::delta(y::xs) |
357 } |
403 } |
383 case class Leaf(x: Int) extends Tree |
429 case class Leaf(x: Int) extends Tree |
384 case class Node(s: String, left: Tree, right: Tree) extends Tree |
430 case class Node(s: String, left: Tree, right: Tree) extends Tree |
385 |
431 |
386 val lf = Leaf(20) |
432 val lf = Leaf(20) |
387 val tr = Node("foo", Leaf(10), Leaf(23)) |
433 val tr = Node("foo", Leaf(10), Leaf(23)) |
|
434 |
|
435 def sizet(t: Tree) : Int = t match { |
|
436 case Leaf(_) => 1 |
|
437 case Node(_, left , right) => 1 + sizet(left) + sizet(right) |
|
438 } |
|
439 |
|
440 sizet(tr) |
388 |
441 |
389 val lst : List[Tree] = List(lf, tr) |
442 val lst : List[Tree] = List(lf, tr) |
390 |
443 |
391 |
444 |
392 abstract class Colour |
445 abstract class Colour |