* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2.1 KiB
2.1 KiB
id, title, challengeType, videoUrl, dashedName
id | title | challengeType | videoUrl | dashedName |
---|---|---|---|---|
594810f028c0303b75339ad5 | 和组合 | 5 | y-combinator |
--description--
在严格的函数编程和lambda演算中 ,函数(lambda表达式)没有状态,只允许引用封闭函数的参数。这排除了递归函数的通常定义,其中函数与变量的状态相关联,并且该变量的状态在函数体中使用。
Y组合器本身是一个无状态函数,当应用于另一个无状态函数时,它返回函数的递归版本。 Y组合器是这类函数中最简单的一种,称为定点组合器 。
任务: Define the stateless Y combinator function and use it to compute <a href="https://en.wikipedia.org/wiki/Factorial" title="wp: factorial">factorial</a>.
factorial(N)
功能已经给你了。另见Jim Weirich:功能编程中的冒险 。
--hints--
Y必须返回一个函数
assert.equal(typeof Y((f) => (n) => n), 'function');
factorial(1)必须返回1。
assert.equal(factorial(1), 1);
factorial(2)必须返回2。
assert.equal(factorial(2), 2);
factorial(3)必须返回6。
assert.equal(factorial(3), 6);
factorial(4)必须返回24。
assert.equal(factorial(4), 24);
factorial(10)必须返回3628800。
assert.equal(factorial(10), 3628800);
--seed--
--after-user-code--
var factorial = Y(f => n => (n > 1 ? n * f(n - 1) : 1));
--seed-contents--
function Y(f) {
return function() {
};
}
var factorial = Y(function(f) {
return function (n) {
return n > 1 ? n * f(n - 1) : 1;
};
});
--solutions--
var Y = f => (x => x(x))(y => f(x => y(y)(x)));