Skip to content

Instantly share code, notes, and snippets.

@amasad
Created June 6, 2013 15:41
Show Gist options
  • Save amasad/5722523 to your computer and use it in GitHub Desktop.
Save amasad/5722523 to your computer and use it in GitHub Desktop.
cons, car, and cdr using only functions in JS
function cons (a, b) {
return function (sel) {
return sel(a, b)
}
}
function car (l) {
return l(function (a, b) {
return a
})
}
function cdr (l) {
return l(function (a, b) {
return b
})
}
var l = cons(1, cons(2, cons(3, null)));
console.log(
car(l) // 1
, car(cdr(l)) // 2
, car(cdr(cdr(l))) //3
, cdr(cdr(cdr(l))) //null
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment