-
-
Save buzzdecafe/6261503 to your computer and use it in GitHub Desktop.
S Combinator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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; | |
} | |
/* | |
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. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I love it that javascript is inspired by scheme, otherwise I would never, ever, ever understand this. Thank god JS exists.