All examples By author By category About

bmershon

Y Combinator

See Mike Vanier's blog post explaining the Y Combinator as it is introduced in The Little Schemer and derived by Eli Barzilay.

Presented here is a JavaScript implementation of the Y Combinator, which The Little Schemer implements with, well, Scheme. More specifically, the desired flavor of Y combinator is the applicative-order Y Combinator:

Racket

(define Y
  (lambda (f)
    ((lambda (x) (x x))
     (lambda (x) (f (lambda (g) ((x x) g)))))))

JavaScript

var Y = function(fix) {
  return (function(again) {
    return again(again);
  })(function (again) {
    return fix(function(x) {
      return (again(again))(x);
    });  
  });
};

Pay careful attention to these lines.

See also code snippets from my attempts at working through The Little Schemer.