Created
June 6, 2013 15:41
-
-
Save amasad/5722523 to your computer and use it in GitHub Desktop.
cons, car, and cdr using only functions in JS
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
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