Skip to content

Instantly share code, notes, and snippets.

@rustedgrail
Created November 15, 2013 20:41
Show Gist options
  • Save rustedgrail/7491201 to your computer and use it in GitHub Desktop.
Save rustedgrail/7491201 to your computer and use it in GitHub Desktop.
function emptySet(x) {
return false;
}
function insert(elem, set) {
return function(x) {
if (x === elem) { // insert smarter comparison here if needed
return true;
}
return function() { return contains(x, set); };
};
}
function contains(elem, set) {
var retVal = set(elem);
while(typeof retVal === 'function') {
retVal = retVal(elem);
}
return retVal;
}
var aList = insert(3, insert(2, insert(1, emptySet)));
console.log("FIRST: " + contains(2, aList));
console.log("SECOND: " + contains(4, aList));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment