mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-04-13 13:00:15 -04:00
2.0 KiB
2.0 KiB
id, title, challengeType, forumTopicId, dashedName
| id | title | challengeType | forumTopicId | dashedName |
|---|---|---|---|---|
| 594810f028c0303b75339ad5 | Y コンビネータ | 1 | 302345 | y-combinator |
--description--
In strict functional programming and the lambda calculus, functions (lambda expressions) don't have state and are only allowed to refer to arguments of enclosing functions. This rules out the usual definition of a recursive function wherein a function is associated with the state of a variable and this variable's state is used in the body of the function.
Y コンビネータ は、それ自体がステートレス関数であり、他のステートレス関数に適用されると、関数の再帰バージョンを返します。 Y コンビネータは、不動点コンビネータと呼ばれるこのような関数のクラスの最も単純なものです。
--instructions--
ステートレスな Y コンビネータ関数を定義し、それを使用して階乗を計算してください。 The factorial(N) function is already given to you.
--hints--
Y should return a function.
assert.equal(typeof Y((f) => (n) => n), 'function');
factorial(1) should return 1.
assert.equal(factorial(1), 1);
factorial(2) should return 2.
assert.equal(factorial(2), 2);
factorial(3) should return 6.
assert.equal(factorial(3), 6);
factorial(4) should return 24.
assert.equal(factorial(4), 24);
factorial(10) should return 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)));