--- a/progs/comb1.scala Mon Oct 24 14:46:47 2016 +0100
+++ b/progs/comb1.scala Sat Oct 29 21:45:44 2016 +0100
@@ -156,3 +156,41 @@
(One ~ One ~ One ~ One).parse("1111")
(One || Two).parse("111")
+
+
+for (x <- List(1, 2, 3, 4)) println(x)
+for (x <- List(1, 2, 3, 4); if (2 < x)) yield (x.toString + x.toString)
+for (x <- List("2", "1", "3", "4", "1")) yield (x + x + x)
+
+(1, "one", '1')._3
+for ((x, y) <- List((1, "one"), (2, "two"), (3, "three"), (4,"many")); if (y == "many"))
+ yield (x.toString + y)
+
+
+def square(n: Int) = {
+ n * n
+}
+
+square(4 + 3 + 5)
+
+def bar(): Int = {
+ bar()
+ 3
+}
+
+
+def foo(n: => Int) = {
+ print("finished")
+}
+
+foo(bar())
+
+square(12) + square(10)
+
+
+def time_needed[T](i: Int, code: => T) = {
+ val start = System.nanoTime()
+ for (j <- 1 to i) code
+ val end = System.nanoTime()
+ (end - start)/(i * 1.0e9)
+}
--- a/progs/re1.scala Mon Oct 24 14:46:47 2016 +0100
+++ b/progs/re1.scala Sat Oct 29 21:45:44 2016 +0100
@@ -62,9 +62,9 @@
val EVIL2 = SEQ(STAR(STAR(CHAR('a'))), CHAR('b'))
//for measuring time
-def time_needed[T](i: Int, code: => T) = {
+def time_needed[T](i: Int, code => T) = {
val start = System.nanoTime()
- for (j <- 1 to i) code
+ for (j <- 1 to i) code()
val end = System.nanoTime()
(end - start)/(i * 1.0e9)
}
--- a/progs/re3.scala Mon Oct 24 14:46:47 2016 +0100
+++ b/progs/re3.scala Sat Oct 29 21:45:44 2016 +0100
@@ -8,6 +8,7 @@
case class STAR(r: Rexp) extends Rexp
case class NTIMES(r: Rexp, n: Int) extends Rexp
+
// nullable function: tests whether the regular
// expression can recognise the empty string
def nullable (r: Rexp) : Boolean = r match {
@@ -47,7 +48,6 @@
case (r1s, ONE) => r1s
case (r1s, r2s) => SEQ(r1s, r2s)
}
- case NTIMES(r1, n) => NTIMES(simp(r1), n)
case r => r
}
--- a/progs/re4.scala Mon Oct 24 14:46:47 2016 +0100
+++ b/progs/re4.scala Sat Oct 29 21:45:44 2016 +0100
@@ -57,7 +57,7 @@
case (s, ONE) => if (s == Nil) ONE else ZERO
case (s, CHAR(c)) => if (s == List(c)) ONE else
if (s == Nil) CHAR(c) else ZERO
- case (s, ALT(r1, r2)) => ALT(ders2(s, r2), ders2(s, r2))
+ case (s, ALT(r1, r2)) => ALT(ders2(s, r1), ders2(s, r2))
case (c::s, r) => ders2(s, simp(der(c, r)))
}