Files
freeCodeCamp/curriculum/challenges/german/22-rosetta-code/rosetta-code-challenges/y-combinator.md
2024-01-24 19:52:36 +01:00

1.9 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
594810f028c0303b75339ad5 Y Kombinator 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.

Der Y-Kombinator ist selbst eine zustandslose Funktion, die bei Anwendung auf eine andere zustandslose Funktion eine rekursive Version der Funktion zurückgibt. Der Y-Kombinator ist die einfachste Klasse solcher Funktionen, die Fixpunkt-Kombinatoren genannt werden.

--instructions--

Definiere die zustandslose Y-Kombinatorfunktion und verwende diese zur Berechnung der Fakultäten. Die factorial(N)-Funktion ist bereits gegeben.

--hints--

Y sollte eine Funktion zurückgeben.

assert.equal(typeof Y((f) => (n) => n), 'function');

factorial(1) sollte 1 zurückgeben.

assert.equal(factorial(1), 1);

factorial(2) sollte 2 zurückgeben.

assert.equal(factorial(2), 2);

factorial(3) sollte 6 zurückgeben.

assert.equal(factorial(3), 6);

factorial(4) sollte 24 zurückgeben.

assert.equal(factorial(4), 24);

factorial(10) sollte 3628800 zurückgeben.

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)));