author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Tue, 26 Feb 2013 13:24:40 +0000 | |
changeset 198 | d93cc4295306 |
parent 195 | f06aa4e1c25b |
child 200 | 8dde2e46c69d |
permissions | -rw-r--r-- |
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1 |
package object recs { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
2 |
|
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
3 |
//Recursive Functions |
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
4 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
5 |
abstract class Rec { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
def eval(ns: List[Int]) : Int |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
7 |
} |
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
8 |
|
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
9 |
case object Z extends Rec { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
10 |
override def eval(ns: List[Int]) = ns match { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
11 |
case n::Nil => 0 |
198
d93cc4295306
tuned some files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
195
diff
changeset
|
12 |
case _ => throw new IllegalArgumentException("Z args: " + ns) |
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
} |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
14 |
} |
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
15 |
|
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
16 |
case object S extends Rec { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
override def eval(ns: List[Int]) = ns match { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
18 |
case n::Nil => n + 1 |
198
d93cc4295306
tuned some files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
195
diff
changeset
|
19 |
case _ => throw new IllegalArgumentException("S args: " + ns) |
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
20 |
} |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
21 |
} |
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
22 |
|
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
23 |
case class Id(n: Int, m: Int) extends Rec { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
24 |
override def eval(ns: List[Int]) = |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
25 |
if (ns.length == n && m < n) ns(m) |
198
d93cc4295306
tuned some files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
195
diff
changeset
|
26 |
else throw new IllegalArgumentException("Id args: " + ns + "," + n + "," + m) |
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
27 |
} |
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
28 |
|
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
29 |
case class Cn(n: Int, f: Rec, gs: List[Rec]) extends Rec { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
30 |
override def eval(ns: List[Int]) = |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
31 |
if (ns.length == n) f.eval(gs.map(_.eval(ns))) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
32 |
else throw new IllegalArgumentException("Cn: args") |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
33 |
} |
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
34 |
|
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
35 |
case class Pr(n: Int, f: Rec, g: Rec) extends Rec { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
36 |
override def eval(ns: List[Int]) = |
195
f06aa4e1c25b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
194
diff
changeset
|
37 |
if (ns.length == n + 1) { |
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
38 |
if (ns.last == 0) f.eval(ns.init) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
39 |
else { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
40 |
val r = Pr(n, f, g).eval(ns.init ::: List(ns.last - 1)) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
41 |
g.eval(ns.init ::: List(ns.last - 1, r)) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
42 |
} |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
43 |
} |
195
f06aa4e1c25b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
194
diff
changeset
|
44 |
else throw new IllegalArgumentException("Pr: args") |
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
45 |
} |
194
fc2a5e9fbb97
updated Scala files
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
193
diff
changeset
|
46 |
|
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
47 |
case class Mn(n: Int, f: Rec) extends Rec { |
195
f06aa4e1c25b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
194
diff
changeset
|
48 |
def evaln(ns: List[Int], n: Int) : Int = |
f06aa4e1c25b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
194
diff
changeset
|
49 |
if (f.eval(ns ::: List(n)) == 0) n else evaln(ns, n + 1) |
f06aa4e1c25b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
194
diff
changeset
|
50 |
|
f06aa4e1c25b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
194
diff
changeset
|
51 |
override def eval(ns: List[Int]) = |
f06aa4e1c25b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
194
diff
changeset
|
52 |
if (ns.length == n) evaln(ns, 0) |
f06aa4e1c25b
tuned
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
194
diff
changeset
|
53 |
else throw new IllegalArgumentException("Mn: args") |
193
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
54 |
} |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
55 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
56 |
} |