Skip to content

Instantly share code, notes, and snippets.

@corlaez
Last active December 19, 2019 15:17
Show Gist options
  • Save corlaez/077348c6da6001fb536fa4a4835f0111 to your computer and use it in GitHub Desktop.
Save corlaez/077348c6da6001fb536fa4a4835f0111 to your computer and use it in GitHub Desktop.
// Y combinator's lambda calculus formula:
// Y = λf.(λx.f(x x))(λx.f(x x))
// Single line Y combinator implementation:
// const Y = f => (x => f(v => x(x)(v))) (x => f(v => x(x)(v)))
// My prefered (2 line) Y implementation (more readable IMO)
const λfx__f_xx_ = f => x => f( y => x(x)(y) ); // helper
const Y = f => λfx__f_xx_(f) ( λfx__f_xx_(f) ); // The Y combinator
// factorial generator for Y (where fact is truly recursive):
const Y_factg = fact => n => n === 0 ? 1 : n * fact(n - 1)
// A factorial function generated with Y:
const Y_fact = Y(Y_factg)
console.log(Y_fact(5)) // 120
// My first U implementation was accidental when I was trying for 'Y'
// // In my defense У is actually the 'U' sound in russian (just kidding I barely know any Russian)
// U = λf.f f
const U = f => f(f) // The U combinator
// factorial generator for U (where factU is recieves itself legally):
var U_factg = factU => n => n === 0 ? 1 : n * factU(factU)(n - 1)
const U_fact = U(U_factg)
console.log(U_fact(6)) //720
// Conclusion:
// I know that The Y combinator generator recieves as a first parameter a true recursive function
// and that may very well be part of Y's awesomeness and hype.
// However I consider U a legal, more concise and perfectly capable way to resolve for the same problems, AIR?
// -> if there is situations that Y can solve that U can not, let me know.
// I am still very new at lambda so if you have any comment or don't agree with me let me know.
// So will we ever see U tattoos?
// U = λf.f f
// Sadly it's less flashy.
@corlaez
Copy link
Author

corlaez commented Jan 12, 2019

I was re reading this file and russian y (u sound) looks just like λ inverted, mind blown.

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