递归:在变量中存储多个值 [英] recursion: storage of multiple values in variables

查看:32
本文介绍了递归:在变量中存储多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码显示了一个递归函数,它接受一个数字,比如 n=5,并返回一个从 n 到 1 倒计时的数组,即 [5,4,3,2,1].

Code shows a recursive function that takes a number, say n=5, and returns an array counting down from n to 1 i.e [5,4,3,2,1].

我的困惑就在于我们将 n 的数字/值推送到 countArray 之前.我的理解是 countup(n - 1) 会生成数字(比如 n=5)5,4,3,2,1...但我不明白它们在哪里/如何存储.我原以为 n 最终会成为它最后定义的值,n=1 或一个空白数组.但事实并非如此,它们以某种方式都被推入了一个数组,据我所知,该数组在推入之前从未创建/定义过.所以带注释的这两行是我需要帮助理解的两行.

My confusion lies right before we push the numbers/values of n to countArray. My understanding is that countup(n - 1), will generate numbers (say n=5) 5,4,3,2,1...but I don't understand where/how they are stored. I would have thought n would end up as its last defined value, n=1, or a blank array. But that's not true and they are somehow all pushed into an array that in my understanding was never created/defined prior to pushing into it. So those two lines with comments are the two I need help understanding.

tl;dr: (1) 在推入数组之前,值 5->1 如何存储而不覆盖最终值 1?(2) countArray 在我们推入它之前在哪里/如何定义为一个数组;

tl;dr: (1) how are the values 5->1 stored without overwriting to the final value 1, prior to being pushed into the array? (2) Where/how was countArray defined as an array before we push into it;

function countup(n) {
  if (n < 1) {
    return [];
  } else {
    const countArray = countup(n - 1);  //the storing of 5,4,3,2,1 I don't understand
    countArray.push(n); //I don't understand when countArray was defined as an array
    return countArray;
  }
}
console.log(countup(5)); // [ 1, 2, 3, 4, 5 ]

这篇文章的标题可能需要更改为数组而不是变量等.

推荐答案

也许添加一些日志可能会更清楚.

Perhaps adding some logging might make it more clear.

我们可以添加一个简单的记录器来报告这样的值:

We can add a simple logger that reports the values like this:

Calling with 5
|   Calling with 4
|   |   Calling with 3
|   |   |   Calling with 2
|   |   |   |   Calling with 1
|   |   |   |   |   Calling with 0
|   |   |   |   |   Returning [] (base case)
|   |   |   |   Pushing 1 to []
|   |   |   |   Returning [1]
|   |   |   Pushing 2 to [1]
|   |   |   Returning [1,2]
|   |   Pushing 3 to [1,2]
|   |   Returning [1,2,3]
|   Pushing 4 to [1,2,3]
|   Returning [1,2,3,4]
Pushing 5 to [1,2,3,4]
Returning [1,2,3,4,5]

所以数组是在基本情况下定义的.然后,当我们努力备份调用堆栈时,我们添加了它.有其他方法可以做到这一点,但这是一种常见且合理的方法.

So the array was defined in the base case. Then as we worked our way back up the call stack, we added to it. There are alternative ways of doing this, but this is one common and reasonable way to go about it.

您可以在以下代码段中看到我是如何添加日志的:

You can see how I added the logging in the following snippet:

const log = (depth, message) => 
  console .log ('|   '.repeat (depth - 1)  + message)


function countup(n, depth = 1) {
  log(depth, `Calling with ${n}`)
  if (n < 1) {
    log(depth, `Returning [] (base case)`)
    return [];
  } else {
    const countArray = countup(n - 1, depth + 1);  //the storing of 5,4,3,2,1 I don't understand
    log(depth, `Pushing ${n} to [${countArray}]`)
    countArray.push(n); //I don't understand when countArray was defined as an array
    log(depth, `Returning [${countArray}]`)
    return countArray;
  }
}

countup(5)

.as-console-wrapper {min-height: 100% !important; top: 0}

也许这个结果会更清楚:

Perhaps clearer would be this result:

/ Calling with 5
|   / Calling with 4
|   |   / Calling with 3
|   |   |   / Calling with 2
|   |   |   |   / Calling with 1
|   |   |   |   |   / Calling with 0
|   |   |   |   |   \ Returning [] (base case)
|   |   |   |   | Pushing 1 to []
|   |   |   |   \ Returning [1]
|   |   |   | Pushing 2 to [1]
|   |   |   \ Returning [1,2]
|   |   | Pushing 3 to [1,2]
|   |   \ Returning [1,2,3]
|   | Pushing 4 to [1,2,3]
|   \ Returning [1,2,3,4]
| Pushing 5 to [1,2,3,4]
\ Returning [1,2,3,4,5]

仅涉及对日志记录语句的微小更改:

Which only involves a minor change to the logging statements:

const log = (depth, message) => 
  console .log ('|   '.repeat (depth - 1)  + message)
  
function countup(n, depth = 1) {
  log(depth, `/ Calling with ${n}`)
  if (n < 1) {
    log(depth, `\\ Returning [] (base case)`)
    return [];
  } else {
    const countArray = countup(n - 1, depth + 1);  //the storing of 5,4,3,2,1 I don't understand
    log(depth, `| Pushing ${n} to [${countArray}]`)
    countArray.push(n); //I don't understand when countArray was defined as an array
    log(depth, `\\ Returning [${countArray}]`)
    return countArray;
  }
}

countup(5)

.as-console-wrapper {min-height: 100% !important; top: 0}

这篇关于递归:在变量中存储多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆