Skip to content

Instantly share code, notes, and snippets.

@marshallshen
Created October 9, 2013 01:26
Show Gist options
  • Save marshallshen/6894708 to your computer and use it in GitHub Desktop.
Save marshallshen/6894708 to your computer and use it in GitHub Desktop.
Piano Numbers serves as the basis for floating numbers
package pianoNumbers
abstract class Nat {
def isZero: Boolean
def successor = new Succ(this)
def successor: Nat
def +(that: Nat): Nat
def -(+that: Nat): Nat
}
object Zero extends Nat {
def isZero = true
def predecessor = throw new Error("0.predecessor")
def successor = new Succ(this)
def + (that: Nat) = that
def - (that: Nat) = if (that.isZero) this else throw new Error("negative number")
}
class Succ(n: Nat) extends Nat {
def isZero = false
def predecessor = n
def + (that: Nat) = new Succ(n + that)
def - (that: Nat) = if(that.isZero) this else n - that.predecessor
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment