Cons, Car, and Cdr: The Javascript Way

I recently watched most of the Structure and Interpretation of Computer Programs (SICP) lectures and along the way I learned about the completely data-less representation available for Lisp's cons car and cdr functions. It was pretty mind blowing and just so I could feel smart for a couple minutes I ported it to Javascript, a language with almost all of Lisp's cool features but with C's syntax.

function cons(x, y) {
  return function(m) {
    m(x, y);
  };
}

function car(x) {
  x(function(a,d) {
    return a;
  });
}

function cdr(x) {
  x(function(a,d) {
    return d;
  });
}

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

Captcha
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
2 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.