author | Christian Urban <christian dot urban at kcl dot ac dot uk> |
Fri, 22 Feb 2013 14:31:34 +0000 | |
changeset 194 | fc2a5e9fbb97 |
parent 193 | 317a2532c567 |
child 195 | f06aa4e1c25b |
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 |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
12 |
case _ => throw new IllegalArgumentException("Z: args") |
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 |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
19 |
case _ => throw new IllegalArgumentException("S: args") |
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) |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
26 |
else throw new IllegalArgumentException("Id: args") |
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]) = |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
37 |
if (ns.length == n - 1) { |
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 |
} |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
44 |
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
|
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 { |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
48 |
override def eval(ns: List[Int]) = 0 |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
49 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
50 |
} |
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
51 |
|
317a2532c567
split up scala-file into separate components
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
52 |
} |