Skip to content

Instantly share code, notes, and snippets.

@buzzdecafe
Last active April 18, 2024 11:55
Show Gist options
  • Save buzzdecafe/6261503 to your computer and use it in GitHub Desktop.
Save buzzdecafe/6261503 to your computer and use it in GitHub Desktop.
S Combinator
/*
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.
*/
@Kreijstal
Copy link

I love it that javascript is inspired by scheme, otherwise I would never, ever, ever understand this. Thank god JS exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment