--- a/scala/abacus.scala Fri Mar 08 11:31:04 2013 +0000
+++ b/scala/abacus.scala Sun Mar 10 07:10:50 2013 +0000
@@ -35,16 +35,16 @@
case Goto(l) => if (l == old_jump) Goto(jump) else Goto(l)
}))
- def step(cf: AConfig) : AConfig = (nth_of(p, cf.s), cf.s) match {
- case (None, _) => cf
- case (Some(Inc(n)), s) => AConfig(s + 1, cf.regs + (n -> (dget(cf.regs, n) + 1)))
- case (Some(Dec(n, l)), s) => {
+ def step(cf: AConfig) : AConfig = nth_of(p, cf.s) match {
+ case None => cf
+ case Some(Inc(n)) => AConfig(cf.s + 1, cf.regs + (n -> (dget(cf.regs, n) + 1)))
+ case Some(Dec(n, l)) => {
if (dget(cf.regs, n) == 0) AConfig(l, cf.regs)
- else AConfig(s + 1, cf.regs + (n -> (dget(cf.regs, n) - 1)))
+ else AConfig(cf.s + 1, cf.regs + (n -> (dget(cf.regs, n) - 1)))
}
- case (Some(Goto(l)), _) => AConfig(l, cf.regs)
- }
-
+ case Some(Goto(l)) => AConfig(l, cf.regs)
+ }
+
def steps(cf: AConfig, n: Int) : AConfig = n match {
case 0 => cf
case n => steps(step(cf), n - 1)
--- a/scala/ex.scala Fri Mar 08 11:31:04 2013 +0000
+++ b/scala/ex.scala Sun Mar 10 07:10:50 2013 +0000
@@ -90,8 +90,11 @@
def test_comp2(f: Rec, ns: Int*) = {
val (abc_f, arity, _) = compile_rec(f)
val abc_map = (0 until ns.length).zip(ns).toMap[Int, Int]
+ val start = System.nanoTime()
val res = (abc_f.run(abc_map))(arity)
- ("Result: " + res + " length: " + abc_f.p.length)
+ val end = System.nanoTime()
+ val time = (end - start)/1.0e9
+ ("Result: " + res + " length: " + abc_f.p.length + " time: " + "%.5f".format(time))
}
println("S(3) " + test_comp2(S, 3))
@@ -102,7 +105,7 @@
println("Power(3, 4) " + test_comp2(Power, 3, 4))
println("Minus(30, 4) " + test_comp2(Minus, 30, 4))
println("Fact(5) " + test_comp2(Fact, 5))
-//println("Prime(5) " + test_comp2(Prime, 5))
+println("Prime(5) " + test_comp2(Prime, 5))
//println("Prime(4) " + test_comp2(Prime, 4))
println("Strt(1)(2) " + test_comp2(Strt(1), 2))
@@ -110,8 +113,11 @@
def test_comp1(f: Rec, ns: Int*) = {
val (abc_f, arity, _) = compile_rec(f)
val tm = toTM(abc_f.p) :+ TMMopup(arity)
+ val start = System.nanoTime()
val res = tm.run(Tape(ns.toList))
- ("length: " + tm.p.length + " tape: " + arity + "\nResult: " + res)
+ val end = System.nanoTime()
+ val time = (end - start)/1.0e9
+ ("length: " + tm.p.length + " tape: " + arity + " time: " + "%.5f".format(time) + "\nResult: " + res)
}
println("")
--- a/scala/lib.scala Fri Mar 08 11:31:04 2013 +0000
+++ b/scala/lib.scala Sun Mar 10 07:10:50 2013 +0000
@@ -1,15 +1,23 @@
package object lib {
//some list functions
-def tl[A](xs: List[A]) : List[A] = xs match {
- case Nil => Nil
- case x::xs => xs
-}
+//slow version
+//def tl[A](xs: List[A]) : List[A] = xs match {
+// case Nil => Nil
+// case x::xs => xs
+//}
+
+def tl[A](xs: List[A]) : List[A] =
+ try { xs.tail } catch { case _:UnsupportedOperationException => Nil }
def hd[A](xs: List[A]) : A = xs.head
+//slow version
+//def nth_of[A](xs: List[A], n: Int) =
+// if (n <= xs.length) Some(xs(n)) else None
+
def nth_of[A](xs: List[A], n: Int) =
- if (n <= xs.length) Some(xs(n)) else None
+ try { Some(xs(n)) } catch { case _:IndexOutOfBoundsException => None }
//some map functions
def dget(m: Map[Int, Int], n: Int) = m.getOrElse(n, 0)