scala/recs.scala
changeset 195 f06aa4e1c25b
parent 194 fc2a5e9fbb97
child 198 d93cc4295306
equal deleted inserted replaced
194:fc2a5e9fbb97 195:f06aa4e1c25b
    32     else throw new IllegalArgumentException("Cn: args")
    32     else throw new IllegalArgumentException("Cn: args")
    33 }
    33 }
    34 
    34 
    35 case class Pr(n: Int, f: Rec, g: Rec) extends Rec {
    35 case class Pr(n: Int, f: Rec, g: Rec) extends Rec {
    36   override def eval(ns: List[Int]) = 
    36   override def eval(ns: List[Int]) = 
    37     if (ns.length == n - 1) {
    37     if (ns.length == n + 1) {
    38       if (ns.last == 0) f.eval(ns.init)
    38       if (ns.last == 0) f.eval(ns.init)
    39       else {
    39       else {
    40         val r = Pr(n, f, g).eval(ns.init ::: List(ns.last - 1))
    40         val r = Pr(n, f, g).eval(ns.init ::: List(ns.last - 1))
    41         g.eval(ns.init ::: List(ns.last - 1, r))
    41         g.eval(ns.init ::: List(ns.last - 1, r))
    42       }
    42       }
    43     }
    43     }
    44     else throw new IllegalArgumentException("Cn: args")
    44     else throw new IllegalArgumentException("Pr: args")
    45 }
    45 }
    46 
    46 
    47 case class Mn(n: Int, f: Rec) extends Rec {
    47 case class Mn(n: Int, f: Rec) extends Rec {
    48   override def eval(ns: List[Int]) = 0
    48   def evaln(ns: List[Int], n: Int) : Int = 
    49     
    49     if (f.eval(ns ::: List(n)) == 0) n else evaln(ns, n + 1)
       
    50 
       
    51   override def eval(ns: List[Int]) = 
       
    52     if (ns.length == n) evaln(ns, 0) 
       
    53     else throw new IllegalArgumentException("Mn: args")
    50 }
    54 }
    51 
    55 
    52 }
    56 }