Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* 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>
2021-01-12 19:31:00 -07:00

80 lines
2.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
id: 594810f028c0303b75339ad5
title: 和组合
challengeType: 5
videoUrl: ''
dashedName: y-combinator
---
# --description--
<p>在严格的<a href='https://en.wikipedia.org/wiki/Functional programming' title='wp函数式编程'>函数编程</a><a href='https://en.wikipedia.org/wiki/lambda calculus' title='wplambda演算'>lambda演算中</a> 函数lambda表达式没有状态只允许引用封闭函数的参数。这排除了递归函数的通常定义其中函数与变量的状态相关联并且该变量的状态在函数体中使用。 </p><p> <a href='http://mvanier.livejournal.com/2897.html'>Y组合</a>器本身是一个无状态函数,当应用于另一个无状态函数时,它返回函数的递归版本。 Y组合器是这类函数中最简单的一种称为<a href='https://en.wikipedia.org/wiki/Fixed-point combinator' title='wp定点组合器'>定点组合器</a></p>任务: <pre> <code>Define the stateless Y combinator function and use it to compute &#x3C;a href="https://en.wikipedia.org/wiki/Factorial" title="wp: factorial">factorial&#x3C;/a>.</code> </pre><p> <code>factorial(N)</code>功能已经给你了。另见<a href='http://vimeo.com/45140590'>Jim Weirich功能编程中的冒险</a></p>
# --hints--
Y必须返回一个函数
```js
assert.equal(typeof Y((f) => (n) => n), 'function');
```
factorial1必须返回1。
```js
assert.equal(factorial(1), 1);
```
factorial2必须返回2。
```js
assert.equal(factorial(2), 2);
```
factorial3必须返回6。
```js
assert.equal(factorial(3), 6);
```
factorial4必须返回24。
```js
assert.equal(factorial(4), 24);
```
factorial10必须返回3628800。
```js
assert.equal(factorial(10), 3628800);
```
# --seed--
## --after-user-code--
```js
var factorial = Y(f => n => (n > 1 ? n * f(n - 1) : 1));
```
## --seed-contents--
```js
function Y(f) {
return function() {
};
}
var factorial = Y(function(f) {
return function (n) {
return n > 1 ? n * f(n - 1) : 1;
};
});
```
# --solutions--
```js
var Y = f => (x => x(x))(y => f(x => y(y)(x)));
```