Created
October 9, 2013 01:26
-
-
Save marshallshen/6894708 to your computer and use it in GitHub Desktop.
Piano Numbers serves as the basis for floating numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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