Upgrade to Pro — share decks privately, control downloads, hide ads and more …

As Little As Possible

As Little As Possible

FizzBuzz, in lambda calculus, in JavaScript.

John S.

June 10, 2014
Tweet

More Decks by John S.

Other Decks in Programming

Transcript

  1. FizzBuzz • Print the numbers 1–100; • Replace numbers divisible

    by 3 with “Fizz”; • Replace numbers divisible by 5 with “Buzz”; • Replace numbers divisible by both 3 & 5 with “FizzBuzz”.
  2. FizzBuzz • 1, 2, Fizz, 4, Buzz, Fizz, 7, 8,

    Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz, 16, 17, Fizz, 19, Buzz, Fizz …
  3. FizzBuzz var fizzbuzz = function () { var i, s;

    for (i = 1; i <= 100; i++) { s = ''; if (i % 3 === 0) s += 'Fizz'; if (i % 5 === 0) s += 'Buzz'; if (s === '') s += i; console.log(s); } };
  4. True & False var TRUE = function (l, r) {

    return l; }; var FALSE = function (l, r) { return r; }; var IF = function (f, e1, e2) { return f(e1, e2)(); };
  5. Lists • The empty list; • Prepend to a list;

    • Get the head of a list; • Get the tail of a list; • Check if it’s the empty list.
  6. Lists var EMPTY_LIST = function (selector) { return selector(undefined, undefined,

    TRUE); }; var PREPEND = function (h, t) { return function (selector) { return selector(h, t, FALSE); }; }; var IS_EMPTY = function (list) { return list(function (h, t, e) { return e; }); };
  7. Lists var HEAD = function (list) { return list(function (h,

    t) { return h; }); }; var TAIL = function (list) { return list(function (h, t) { return t; }); };
  8. Logic var AND = function (a, b) { return IF(

    a, function () { return b; }, function () { return FALSE; } ); }; var OR = function (a, b) { return IF( a, function () { return TRUE; }, function () { return b; } ); };
  9. Numbers var ZERO = EMPTY_LIST; var ONE = PREPEND(EMPTY_LIST, ZERO);

    var TWO = PREPEND(EMPTY_LIST, ONE); var INC = function (n) { return PREPEND(EMPTY_LIST, n); }; var DEC = function (n) { return TAIL(n); }; var IS_ZERO = function (n) { return IS_EMPTY(n); };
  10. Addition var ADD = function (a, b) { return IF(

    IS_ZERO(b), function () { return a; }, function () { return ADD(INC(a), DEC(b)); } ); };
  11. Addition var ADD = function (a, b) { return IF(

    IS_ZERO(b), function () { return a; }, function () { return ADD(INC(a), DEC(b)); } ); };
  12. Y–combinator var Y = function (f) { return (function (x)

    { return f(function (y) { return (x(x))(y); }); })(function (x) { return f(function (y) { return (x(x))(y); }); }); };
  13. Addition var ADD = Y(function (g) { return function (n)

    { return function (m) { return IF( IS_ZERO(m), function () { return n; }, function () { return g(INC(n))(DEC(m)); } ); }; }; });
  14. Subtraction var SUB = Y(function (g) { return function (n)

    { return function (m) { return IF( IS_ZERO(m), function () { return n; }, function () { return g(DEC(n))(DEC(m)); } ); }; }; });
  15. < var LESS_THAN = Y(function (g) { return function (m)

    { return function (n) { return IF( AND(IS_ZERO(m), IS_ZERO(n)), function () { return FALSE; }, function () { return IF( IS_ZERO(m), function () { return TRUE; }, function () { return IF( IS_ZERO(n), function () { return FALSE; }, function () { return g(DEC(m))(DEC(n)); } ); } ); } ); }; }; });
  16. REM var REM = Y(function (g) { return function (n)

    { return function (d) { return IF( LESS_THAN(n)(d), function () { return n; }, function () { return REM(SUB(n)(d))(d); } ); }; }; });
  17. Fizz & Buzz & FizzBuzz var FIZZ = function (n)

    { return IF( IS_EQUAL(REM(n)(THREE))(ZERO), function () { return TRUE; }, function () { return FALSE; } ); };
  18. 1–100 var ONE_TO_ONE_HUNDRED = PREPEND( ONE, PREPEND( TWO, PREPEND( THREE,

    PREPEND( FOUR, PREPEND( FIVE, PREPEND( SIX, PREPEND( SEVEN, PREPEND( EIGHT, PREPEND( NINE, PREPEND( ADD(MUL(TEN)(ONE))(ZERO), PREPEND( ADD(MUL(TEN)(ONE))(ONE), PREPEND( ADD(MUL(TEN)(ONE))(TWO), PREPEND( ADD(MUL(TEN)(ONE))(THREE), PREPEND( …
  19. FizzBuzz MAP( function (n) { return IF( FIZZBUZZ(n), function ()

    { console.log('FizzBuzz'); }, function () { return IF( FIZZ(n), function () { console.log('Fizz'); }, function () { return IF( BUZZ(n), function () { console.log('Buzz'); }, function () { console.log(TO_NUMBER(n)); } ); } ); } ); } )(ONE_TO_ONE_HUNDRED);
  20. More • Steve Losh–List Out of Lambda • Tom Stuart–Programming

    with Nothing • Greg Michaelson–Functional Programming Through Lambda Calculus • Jim Wierich–Adventures in Functional Programming
  21. The Whole Thing • Wrote a compiler an hour ago.

    • It’s broken—maybe after James’ talk. • 37KLOC.