Skip to content

Instantly share code, notes, and snippets.

@scotthaleen
Created February 10, 2016 16:31
Show Gist options
  • Save scotthaleen/ae5aad9b2296d4eb854b to your computer and use it in GitHub Desktop.
Save scotthaleen/ae5aad9b2296d4eb854b 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)));
@anhdoecourier
Copy link

const car = (arr) => {
return arr[0];
};

const cdr = (arr) => {
const [, ...a] = arr;
return a;
};

const cons = (a, list) => {
return [a, ...list];
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment