Skip to content

Instantly share code, notes, and snippets.

@jremmen
Created June 6, 2014 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jremmen/870f33ee26a840e3e44b to your computer and use it in GitHub Desktop.
Save jremmen/870f33ee26a840e3e44b to your computer and use it in GitHub Desktop.
js: cons list
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