Created
June 6, 2014 21:41
-
-
Save jremmen/870f33ee26a840e3e44b to your computer and use it in GitHub Desktop.
js: cons list
This file contains hidden or 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 List = function() { | |
return (function build(xs) { | |
if(xs.length === 0) return Nil | |
return new Cons(xs[0], build(Array.prototype.slice.call(xs, 1))); | |
})(arguments); | |
} | |
var Nil = { | |
head: null, | |
tail: null, | |
cons: function(x) { return new Cons(x, Nil); } | |
} | |
var Cons = function(head, tail) { | |
this.head = head; | |
this.tail = tail; | |
this.cons = function(x) { return new Cons(x, this); } | |
return this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment