Skip to content

Instantly share code, notes, and snippets.

@buzzdecafe
Last active April 18, 2024 11:55

Revisions

  1. buzzdecafe revised this gist Oct 16, 2016. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions S.js
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,20 @@
    /*
    S : \x y z -> x z (y z)
    */
    // S :: (z -> (a -> b)) -> (z -> a) -> z -> b
    function S(x, y, z) {
    return x(z)(y(z));
    }

    // example:
    // add :: a -> a -> a
    function add(x) {
    return function(y) {
    return x + y;
    }
    }

    // mult :: a -> a
    function mult3(x) {
    return x * 3;
    }
  2. buzzdecafe created this gist Aug 18, 2013.
    31 changes: 31 additions & 0 deletions S.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    /*
    S : \x y z -> x z (y z)
    */
    function S(x, y, z) {
    return x(z)(y(z));
    }

    // example:
    function add(x) {
    return function(y) {
    return x + y;
    }
    }

    function mult3(x) {
    return x * 3;
    }

    /*
    S: x(z)(y(z));
    x(10)(y(10)) // sub 10 for z
    x(10)(mult3(10)) // sub mult3 for y
    add(10)(30); // eval mult3(10) -> 30; sub add for x
    40 // eval add(10)(30) -> 40
    */
    S(add, mult3, 10); // => 40

    /*
    but what does it mean? why the S combinator? Who cares? How is it useful?
    Appears useful mostly for combinatory logic, not so much for real code. At least not directly.
    */