JSFiddle - React, Tailwind, and code Playground
JavaScript
const z = function() {
// Mathematically, this could be expressed as a simple pair
return {
// We remember at which number we currently are: we are now at 0.
prevNumber: 0,
// Our current factorial result
currentResult: 1
};
};
const succ = function(x) {
// Remember: x is the evaluation of inner succ()s and z() calls!
return {
// Remember to keep track!
prevNumber: x.prevNumber + 1,
// The factorial definition would usually be f(n) = n * f(n-1)
// and this is exactly what we are doing here
currentResult: (x.prevNumber + 1) * x.currentResult
}
};
// This would give
// {
// prevNumber: 3
// currentResult: 6
// }
const result = succ(succ(succ(z())));
const finalResult = result.currentResult;
alert(finalResult);