Created
October 8, 2013 20:49
-
-
Save kaaloo/6891400 to your computer and use it in GitHub Desktop.
Javascript port of https://gist.github.com/kaaloo/6884209
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
var Zero = { | |
isZero: function() { | |
return true; | |
}, | |
predecessor: function() { | |
throw "No such element"; | |
}, | |
successor: function() { | |
return new Succ(this); | |
}, | |
add: function(that) { | |
return that; | |
}, | |
sub: function(that) { | |
if (that.isZero()) { | |
return this; | |
} else { | |
return predecessor().sub(that.predecessor()) | |
} | |
} | |
}; | |
function Succ(n) { | |
this.n = n; | |
} | |
Succ.prototype.isZero = function() { | |
return false; | |
} | |
Succ.prototype.predecessor = function() { | |
return this.n; | |
} | |
Succ.prototype.successor = function() { | |
return new Succ(this); | |
} | |
Succ.prototype.add = function(that) { | |
new Succ(n.add(that)) | |
} | |
Succ.prototype.sub = function(that) { | |
if (that.isZero()) { | |
return this; | |
} else { | |
return predecessor().sub(that.predecessor()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment