简体   繁体   English

需要帮助理解这个递归函数教程

[英]Need help understanding this recursive function tutorial

I came across this example of a recursive function in a textbook.我在教科书中遇到了这个递归函数的例子。 I can see there are other threads on this site that ask a question about this very example from Eloquent JavaScript but that user's difficulty in understanding the example was to do with not understanding how functions are invoked in general.我可以看到该站点上还有其他线程询问有关 Eloquent JavaScript 中这个示例的问题,但用户理解示例的困难在于不理解函数的一般调用方式。

function findSolution(target){
  function find(start, history){
    if(start == target)
      return history
    else if(start > target)
      return null
    else 
      return find(start + 5, "(" + history + " + 5)") ||
             find(start * 3, "(" + history + " * 3)") 
    }
    return find(1, "1");
  }

You supply a target number to the findSolution function, and it executes the inner function, find, which calls itself until it finds a way to reach the target number by adding 5 or multiplying by 3. Say I supply the number 13 as a target to reach.您向 findSolution 函数提供一个目标数字,它执行内部函数 find,该函数调用自身,直到找到通过加 5 或乘以 3 来达到目标​​数字的方法。假设我提供数字 13 作为目标抵达。 The parameters increment as follows.参数递增如下。

Start = 1 + 5, History = "(1 + 5)"
Start = 6 + 5, History = "(1 + 5) + 5"
Start = 11 + 5, History = "(1 + 5) + 5) + 5"

Start becomes 16, and this is higher than 13, so this will return null.开始变为 16,这比 13 大,所以这将返回 null。 So the find function tries to multiply start by 3 instead of adding 5, going back to 11...所以 find 函数尝试将 start 乘以 3 而不是加 5,回到 11 ...

Start = 11 * 3, History = "(1 + 5) + 5) * 3"

This is also too high, and will so return null.这也太高了,所以会返回 null。 Surely the function stops here, right??肯定是这个功能到此为止吧??

When I test it out though, console logging the start values each time find recursively calls itself, it continues to call itself until it reaches number 13. The start values the function cycles through after 33 are:但是,当我对其进行测试时,控制台记录每次 find 递归调用自身时的起始值,它会继续调用自身,直到达到第 13 个。函数在 33 之后循环的起始值是:

18
3
8
13

But how is any of this possible?但是这怎么可能呢? Shouldn't the function just return false after it reaches 16 and 33?函数在达到 16 和 33 后不应该只返回 false 吗?

Console.log history just before the base case. Console.log 历史记录就在基本案例之前。 That will help to understand how it works这将有助于了解它是如何工作的

There is a syntax error in your (the) example code.您的 (the) 示例代码中存在语法错误。 It should appear like this:它应该是这样的:

function findSolution(target){
    function find(start, history){
        if(start == target)
            return history
        else if(start > target)
            return null
        else 
            return (find(start + 5, "(" + history + " + 5)") ||
                    find(start * 3, "(" + history + " * 3)"))
    }
    return find(1, "1");
  }

(Note the structure of the last return in the inner function.) (注意内部函数中最后一个 return 的结构。)

When running the updated function with an input of 13, I see this in the console:当以 13 的输入运行更新的函数时,我在控制台中看到:

(((1 * 3) + 5) + 5) (((1 * 3) + 5) + 5)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM