1 year ago
#383822
David542
How would the factorial using y-combinator simplify after repeated substitutions?
I am playing around with understanding how the Y-combinator works in functional programming. I have a basic factorial function which I have translated to:
console.log((f => n => (n===0) ? 1 : n*f(f)(n-1))(f => n => (n===0) ? 1 : n*f(f)(n-1))(5));
// 120
But how exactly does this simplify (to itself) after calling itself repeatedly? For example, after two times it looks like this:
const fact0 = (f => n => (n===0) ? 1 : n*f(f)(n-1))(f => n => (n===0) ? 1 : n*f(f)(n-1));
// 1. substitute "f => n => (n===0) ? 1 : n*f(f)(n-1)" for function call of f
const fact1 = (n => (n===0) ? 1 : n*(f => n => (n===0) ? 1 : n*f(f)(n-1))(f => n => (n===0) ? 1 : n*f(f)(n-1))(n-1));
// 2. ... ?
// 3. ... ?
// Test to make sure results are ok
console.log(fact0(5) === fact1(5))
javascript
functional-programming
lambda-calculus
0 Answers
Your Answer