Skip to content

Instantly share code, notes, and snippets.

@agentcooper
Last active November 17, 2015 10:48
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 agentcooper/e5bc0591367b1328231c to your computer and use it in GitHub Desktop.
Save agentcooper/e5bc0591367b1328231c to your computer and use it in GitHub Desktop.
sicp.js
// node --harmony index.js
var cons = (a, b) => (f) => f(a, b);
var car = (p) => p((p, q) => p);
var cdr = (p) => p((p, q) => q);
var list = (first, ...rest) =>
!first ?
null :
cons(first, list(...rest));
var append = (items0, items1) =>
items0 === null ?
items1 :
cons(car(items0), append(cdr(items0), items1))
var reverse = (items) =>
items === null ?
items :
append(reverse(cdr(items)), list(car(items)));
var printList = (items) =>
items !== null ?
(console.log(car(items)), printList(cdr(items))) :
null;
var l0 = list('a', 'b', 'c', 'd', 'e');
printList(reverse(l0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment