Skip to content

Instantly share code, notes, and snippets.

@keyvanakbary
Last active June 16, 2016 15:24
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 keyvanakbary/601882c851e4ed56b064913e8480fed0 to your computer and use it in GitHub Desktop.
Save keyvanakbary/601882c851e4ed56b064913e8480fed0 to your computer and use it in GitHub Desktop.
const cons = (x, y) => (m) => m(x, y);
const car = (z) => z((p, q) => p);
const cdr = (z) => z((p, q) => q);
const pair = cons(1, 2);
console.log(car(pair), cdr(pair));//1 2
const list = cons(1, cons(2, cons(3, cons(4, null))));
const each = (l, fn) => cdr(l) ? (() => {fn(car(l)); each(cdr(l), fn)})() : fn(car(l));
each(list, console.log);//1 2 3 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment