Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package idealized.scala
- abstract class Nat {
- def isZero: scala.Boolean
- def predecessor: Nat
- def successor: Nat = new Succ(this)
- def + (that: Nat): Nat
- def - (that:Nat): Nat
- }
- object Zero extends Nat {
- override def isZero: scala.Boolean = true
- override def predecessor: Nat = throw new UnsupportedOperationException
- override def +(that: Nat): Nat = that
- override def -(that: Nat): Nat = if (that.isZero) this else throw new UnsupportedOperationException
- override def toString = "0"
- }
- class Succ(n: Nat) extends Nat {
- override def isZero: scala.Boolean = false
- override def predecessor: Nat = n
- override def +(that: Nat): Nat = new Succ(n + that)
- override def -(that: Nat): Nat = if (that.isZero) this else n - that.predecessor
- override def toString = "s(" + predecessor.toString + ")"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement