Skip to content

Instantly share code, notes, and snippets.

@coder054
Forked from scotthaleen/ConsCarCdr.js
Created February 6, 2023 07:58
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 coder054/592516712e44fa0862a7ce7b1e59910e to your computer and use it in GitHub Desktop.
Save coder054/592516712e44fa0862a7ce7b1e59910e to your computer and use it in GitHub Desktop.
JavaScript implementation of cons, car and cdr
function cons(x, y) {
return function(w) { return w(x, y) };
};
function car(z) {
return z(function(x, y) { return x });
};
function cdr(z) {
return z(function(x, y) { return y });
};
var list = cons(1, cons(2, null));
document.writeln( car(list));
document.writeln( car(cdr(list)));
document.writeln( cdr(cdr(list)));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment