--- a/progs/lecture5.scala Tue Dec 03 11:07:09 2019 +0000
+++ b/progs/lecture5.scala Mon Jan 27 10:18:13 2020 +0000
@@ -2,13 +2,12 @@
//=================
-
// Laziness with style
//=====================
// The concept of lazy evaluation doesn’t really
// exist in non-functional languages. C-like languages
-// are strict. To see the difference, consider
+// are (sort of) strict. To see the difference, consider
def square(x: Int) = x * x
@@ -16,12 +15,12 @@
// This is called "strict evaluation".
-// In contrast, say we have a pretty expensive operation:
+// On the contrary, say we have a pretty expensive operation:
def peop(n: BigInt): Boolean = peop(n + 1)
val a = "foo"
-val b = "bar"
+val b = "foo"
if (a == b || peop(0)) println("true") else println("false")
@@ -48,7 +47,7 @@
val primes = generatePrimes(LazyList.from(2))
// the first 10 primes
-primes.take(10).toList
+primes.take(100).toList
time_needed(1, primes.filter(_ > 100).take(3000).toList)
time_needed(1, primes.filter(_ > 100).take(3000).toList)
@@ -113,14 +112,7 @@
}
-def depth(r: Rexp) : Int = r match {
- case ZERO => 0
- case ONE => 0
- case CHAR(_) => 0
- case ALT(r1, r2) => Math.max(depth(r1), depth(r2)) + 1
- case SEQ(r1, r2) => Math.max(depth(r1), depth(r2)) + 1
- case STAR(r1) => depth(r1) + 1
-}
+
//example regular expressions
val digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
@@ -157,7 +149,17 @@
enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(200).force
-enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(5_000_000)
+enum(LazyList(ZERO, ONE, CHAR('a'), CHAR('b'))).take(5_000_000).force
+
+
+def depth(r: Rexp) : Int = r match {
+ case ZERO => 0
+ case ONE => 0
+ case CHAR(_) => 0
+ case ALT(r1, r2) => Math.max(depth(r1), depth(r2)) + 1
+ case SEQ(r1, r2) => Math.max(depth(r1), depth(r2)) + 1
+ case STAR(r1) => depth(r1) + 1
+}
val is =
@@ -176,7 +178,7 @@
def length_string_list(lst: List[String]): Int = lst match {
case Nil => 0
- case x::xs => 1 + length_string_list(xs)
+ case _::xs => 1 + length_string_list(xs)
}
def length_int_list(lst: List[Int]): Int = lst match {
@@ -185,7 +187,7 @@
}
length_string_list(List("1", "2", "3", "4"))
-length_int_list(List(1, 2, 3, 4))
+length_string_list(List(1, 2, 3, 4))
// you can make the function parametric in type(s)
@@ -193,7 +195,7 @@
case Nil => 0
case x::xs => 1 + length(xs)
}
-length(List("1", "2", "3", "4"))
+length[String](List("1", "2", "3", "4"))
length(List(1, 2, 3, 4))
@@ -220,7 +222,7 @@
case Nil => Nil
case x::xs => {
val res = f(x)
- if (acc.contains(res)) distinctBy(xs, f, acc)
+ if (acc.contains(res) distinctBy(xs, f, acc)
else x::distinctBy(xs, f, res::acc)
}
}
@@ -242,7 +244,7 @@
val y = id("hey") // String
val z = id(Set(1,2,3,4)) // Set[Int]
-
+id[+A, -B]
// The type variable concept in Scala can get really complicated.
//
@@ -266,13 +268,13 @@
arr(0) = "Hello World"
-
// (Immutable)
// Object Oriented Programming in Scala
//
// =====================================
-abstract class Animal
+
+abstract class Animal
case class Bird(name: String) extends Animal {
override def toString = name
}
@@ -393,8 +395,6 @@
(1 / 2) + third
-
-
// DFAs in Scala
//===============
import scala.util.Try
@@ -548,11 +548,14 @@
two - one == one
eight - six == two
-
+eight - six
+// problems about equality and type-errors
-List(1, 2, 3).contains("your cup")
+List(1, 2, 3).contains("your cup") // should not compile, but retruns false
+
+List(1, 2, 3) == Vector(1, 2, 3) // again should not compile, but returns true
// I like best about Scala that it lets me often write
@@ -562,9 +565,9 @@
// Puzzlers
-val MONTH = 12
-val DAY = 24
-val (HOUR, MINUTE, SECOND) = (12, 0, 0)
+val month = 12
+val day = 24
+val (hour, min, sec) = (12, 0, 0)
// use lowercase names for variable
@@ -573,14 +576,14 @@
val oneTwo = Seq(1, 2, 3).permutations
if (oneTwo.length > 0) {
- println("Permutations of 1 and 2:")
+ println("Permutations of 1,2 and 3:")
oneTwo.foreach(println)
}
val threeFour = Seq(3, 4, 5).permutations
if (!threeFour.isEmpty) {
- println("Permutations of 3 and 4:")
+ println("Permutations of 3, 4 and 5:")
threeFour.foreach(println)
}
@@ -610,4 +613,4 @@
//================
// does not work anymore in 2.13.0
-val numbers = List("1", "2").toSet() + "3"
\ No newline at end of file
+val numbers = List("1", "2").toSet + "3"