提问者:小点点

有人能向我解释一下将这个递归函数添加到自身是如何工作的吗?


取自这个链接,我在试图解决这个问题时遇到了这个链接。

这是函数(修改了一点以帮助自己理解):

(function(){

    fibonacci = (function () {

        var cache = {};
        return function (n) {
            var cached = cache[n];
            if (cached) {
                console.log('already in the ', cache);
                return cached;
            }
            if (n <= 1) {
                console.log('no 0s or 1s, ', n);
                return n;
            }
            console.log('a brand new ', n, 'consider yourself cached');
            cache[n] = fibonacci(n - 2) + fibonacci(n - 1);
            console.log('current cache: ', cache);
            return cache[n];
        };
    }());

    fibonacci(20);

})();

我稍微修改了一下,试图帮助自己理解,但我迷失了方向,因为输出走向0,然后从0开始增加。我本以为在这个语句中:

cache[n] = fibonacci(n - 2) + fibonacci(n - 1);

fibonacci(n-2)将被评估,然后紧接着fibonacci(n-1)
但即使是这样,我也不明白JavaScript如何将这两个函数添加在一起。

有人能帮我理解一下这是如何工作的吗,或者至少,你能帮我以一种更容易理解的方式重组它吗?

这是输出:

a brand new  20 consider yourself cached 
a brand new  18 consider yourself cached 
a brand new  16 consider yourself cached 
a brand new  14 consider yourself cached 
a brand new  12 consider yourself cached 
a brand new  10 consider yourself cached 
a brand new  8 consider yourself cached 
a brand new  6 consider yourself cached 
a brand new  4 consider yourself cached 
a brand new  2 consider yourself cached 
no 0s or 1s,  0 
no 0s or 1s,  1 
current cache:  Object {2: 1} 
a brand new  3 consider yourself cached 
no 0s or 1s,  1 
already in the  Object {2: 1} 
current cache:  Object {2: 1, 3: 2} 
current cache:  Object {2: 1, 3: 2, 4: 3} 
a brand new  5 consider yourself cached 
already in the  Object {2: 1, 3: 2, 4: 3} 
already in the  Object {2: 1, 3: 2, 4: 3} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8} 
a brand new  7 consider yourself cached 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8} 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21} 
a brand new  9 consider yourself cached 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21} 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55} 
a brand new  11 consider yourself cached 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55} 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144} 
a brand new  13 consider yourself cached 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144} 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377} 
a brand new  15 consider yourself cached 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377} 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987} 
a brand new  17 consider yourself cached 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987} 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584} 
a brand new  19 consider yourself cached 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584} 
already in the  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584, 19: 4181} 
current cache:  Object {2: 1, 3: 2, 4: 3, 5: 5, 6: 8, 7: 13, 8: 21, 9: 34, 10: 55, 11: 89, 12: 144, 13: 233, 14: 377, 15: 610, 16: 987, 17: 1597, 18: 2584, 19: 4181, 20: 6765} 

谢谢,我知道递归可能是一个很大的新手问题,我已经用过几次了,但是理解它是如何工作的让我头晕目眩。


共2个答案

匿名用户

“我不明白JavaScript如何将这两个函数添加在一起。”

JS不添加函数,而是添加从这些函数调用返回的值。假设计算了f(n-2),当调用f(n-1)时,它将通过以下方式计算:

f(n-1) = f(n-2) + f(n-3)

到目前为止,由于我们在等式的右侧计算了这两个值,因此这两个值都将从缓存中获取。

让我们用一个例子来演示它,假设我们要计算f(5):

f(5) = f(4) + f(3)

使用f(3)进行递归调用:

f(3) = f(2) + f(1)

递归调用:

f(1) = 1 and f(2) = cached(f(1)) + f(0) = 1 + 0 = 1

现在我们回到calc f(3)并且我们缓存了值f(2)和f(1),因此:

f(3) = 1 + 1 = 2

回到Calc F(4)

f(4) = f(3) + f(2) = 2 + 1 = 3

再次结束:

f(5) = f(4) + f(3) = 3 + 2 = 5

请密切注意这样一个事实,即一旦您到达递归中的最深点(f(1)),将使用一路备份缓存,并且不会计算任何值,这使得这种实现非常高效!

匿名用户

如果代码被简化,并且从更小的数字开始,例如4,您可能会发现它更容易。以下功能与您最初发布的功能相同:

var cache = {};

function fibonacci(n) {   // assume n = 4
   var cached = cache[n];

第一次通过时,缓存[4]将未定义,因此以下测试评估为false:

    if (cached) {
        console.log('already in the ', cache);
        return cached;
    }

当n=4时,以下也为假:

    if (n <= 1) {
        console.log('no 0s or 1s, ', n);
        return n;
    }

然后执行以下行:

    console.log('a brand new ', n, 'consider yourself cached');  // 4
    cache[n] = fibonacci(n - 2) + fibonacci(n - 1);

这是:

    cache[4] = fibonacci(2) + fibonacci(3);

在上一行中,首先计算左侧,启动cache的“4”属性的创建。它实际上还没有创建,因为语句还没有完成,所以你几乎已经:

cache = {4:undefined};

然后评估右侧,看看将分配什么。由于有运算符,因此必须评估这两个表达式,看看它是被视为加法还是连接。

接下来fibonacci(2)被评估(无论是加法还是连接,评估从左到右进行),所以上面的过程重复,创建:

    cache[2] = fibonacci(0) + fibonacci(1);

你几乎有:

cache = {4:undefined, 2:undefined};

注意到属性尚未实际创建。

现在fibonacci(0)被评估。这次它到达第二个if并返回0,所以没有创建cache['0'],现在你有:

cache[2] = 0 + fibonacci(1);

同样,在计算fibonacci(1)时,第二个if语句执行并返回1,因此您有一个要分配的值,因此创建了一个属性并分配了值:

    cache[2] = 0 + 1; // cache = {2:1, 4:undefined}; 

现在进入下一行:

    console.log('current cache: ', cache);
    return cache[n]; // returns 1;
}

所以现在上一个电话继续:

    cache[4] = 1 + fibonacci(3);

它再次到达:

    cache[3] = fibonacci(1) + fibonacci(2);

如果,则第一个表达式从第一个返回1,因此您必须:

    cache[3] = 1 + fibonacci(2);

第二个表达式到达第一个if,其中cache[2]存在,因此它返回1(即cache[2]的值)并且您有:

    cache[3] = 1 + 1; // cache = {3:1, 3:2, 4:undefined};

然后返回cache[3](即2),所以你回到:

    cache[4] = 1 + 2;

现在有缓存={2:1,3:2,3:3}

您可以将上述内容编写为顺序操作(所有递归函数都可以编写为顺序操作),这在速度很重要的地方很常见,因为顺序操作总是更快。在这种情况下,顺序函数非常简单:

function fibonacci2(n) {
  var fibs = {0:0, 1:1};
  var i = 1;
  while (i < n) {
    fibs[++i] = fibs[i-1] + fibs[i-2];
  }
  return fibs;
}

console.log(fibonacci2(4));