# HG changeset patch # User Christian Urban # Date 1361146736 0 # Node ID 650e7a7a389b171e5bfad585d61b679c28eaf0a1 # Parent 8248f8adf17d91d2aef046a0a6477f56283723ce tuned diff -r 8248f8adf17d -r 650e7a7a389b turing.scala --- a/turing.scala Sat Feb 16 11:02:08 2013 +0000 +++ b/turing.scala Mon Feb 18 00:18:56 2013 +0000 @@ -190,3 +190,48 @@ println("Ab: " + (new Plus(0, 1, 2)).run(Map(0 -> 3, 1 -> 4))) + + + +//Recursive Functions +abstract class Rec { + def eval(ns: List[Int]) : Int +} +case object Z extends Rec { + override def eval(ns: List[Int]) = ns match { + case n::Nil => 0 + case _ => throw new IllegalArgumentException("Z: args") + } +} +case object S extends Rec { + override def eval(ns: List[Int]) = ns match { + case n::Nil => n + 1 + case _ => throw new IllegalArgumentException("S: args") + } +} +case class Id(n: Int, m: Int) extends Rec { + override def eval(ns: List[Int]) = + if (ns.length == n && m < n) ns(m) + else throw new IllegalArgumentException("Id: args") +} +case class Cn(n: Int, f: Rec, gs: List[Rec]) extends Rec { + override def eval(ns: List[Int]) = + if (ns.length == n) f.eval(gs.map(_.eval(ns))) + else throw new IllegalArgumentException("Cn: args") +} +case class Pr(n: Int, f: Rec, g: Rec) extends Rec { + override def eval(ns: List[Int]) = + if (ns.length == n - 1) { + if (ns.last == 0) f.eval(ns.init) + else { + val r = Pr(n, f, g).eval(ns.init ::: List(ns.last - 1)) + g.eval(ns.init ::: List(ns.last - 1, r)) + } + } + else throw new IllegalArgumentException("Cn: args") +} +case class Mn(n: Int, f: Rec) extends Rec { + override def eval(ns: List[Int]) = + +} +