递归内部函数 Golang

huangapple go评论78阅读模式
英文:

Recursive Inner Functions Golang

问题

递归内部函数声明在Go中

这样做是不是有点丑陋?

我目前正在尝试为一个LeetCode问题编写递归的深度优先搜索(对Go语言还不熟悉)。

无法运行:

当我尝试像这样创建和声明我的内部函数时:

outerFunction(node *TreeNode, target int) bool {
  checkSolutions := func(node *TreeNode, total int) bool {
        // ... DFS算法
        checkSolutions(node.Left)
        checkSolutions(node.Right)
    }
    
  return checkSolution(root, 0)
}

我在递归调用时无法访问内部函数,会出现错误!然而,当我首先将函数声明为变量时(如下所示),它可以运行。

可以运行:
outerFunction(node *TreeNode, target int) bool {
  var checkSolution func(*TreeNode, int) bool
  checkSolutions = func(node *TreeNode, total int) bool {
        // ... DFS算法
        checkSolutions(node.Left)
        checkSolutions(node.Right)
    }
    
  return checkSolution(root, 0)
}

这是在Go中声明递归内部函数的最清晰方式吗?出于某种原因,我觉得这种方式有点啰嗦,所以我想发帖看看是否有适用于这种情况的Go语言语法糖我没有注意到。

英文:

Recursive inner function declaration golang

Is it supposed to be ugly?

I'm currently trying to write a recurisve DFS for a leetcode problem (new to Golang)

Doesn't Run:

when I try to create and declare my inner function like this:

outerFunction (node *TreeNode, target int) bool {
  checkSolutions := func(node *TreeNode, total int) bool {
        // ... DFS algo
        checkSolutions(node.Left)
        checkSolutions(node.Right)
    }
    
  return checkSolution(root, 0)
}

I don't have access to the inner function during the recursive call and get an error! However when I declare the function as a variable first (below) it runs

Runs:
outerFunction (node *TreeNode, target int) bool {
  var checkSolution func(*TreeNode, int) bool
  checkSolutions = func(node *TreeNode, total int) bool {
        // ... DFS algo
        checkSolutions(node.Left)
        checkSolutions(node.Right)
    }
    
  return checkSolution(root, 0)
}

Is this the cleanest way to declare recursive inner functions in Go? For some reason it feels a little bit verbose to me so I just wanted to make this post to see if there's any Golang syntactic sugar intended for this situation that I'm missing.

答案1

得分: 1

简而言之,这是你能得到的最好的结果。这里没有更好的东西。

在第一个情况中,你可以清楚地看到函数checkSolutions没有被声明。因此,当你在声明中使用它时,编译器不知道这个函数是什么,它应该做什么(参数和返回值)。

outerFunction (node *TreeNode, target int) bool {
  checkSolutions := func(node *TreeNode, total int) bool {
        // ... DFS算法
        checkSolutions(node.Left)    // 还没有被声明,不能使用
        checkSolutions(node.Right)
    }
    
  return checkSolution(root, 0)
}

在另一种情况下,当你定义了它应该做什么时,编译器可以理解这些定义并相应地进行处理。

英文:

In a gist, this is the best you can get. There is nothing better here.

In the firat case, you can clearly see that the function checkSolutions is not decleared. Hence, when you use that inside the declaration, go compiler has no idea what that function is and what is it supposed to do (params and returns).

outerFunction (node *TreeNode, target int) bool {
  checkSolutions := func(node *TreeNode, total int) bool {
        // ... DFS algo
        checkSolutions(node.Left)    // Is not decleared yet to be used
        checkSolutions(node.Right)
    }
    
  return checkSolution(root, 0)
}

In other case, when you have defined what it is supposed to do, compiler can understand the definitions and proceed accordingly.

答案2

得分: 0

我相信在snippet1中,在给变量赋值之前,:=右侧的所有语句都必须被评估。一旦评估完成,就可以分配内存。

在snippet1中,我们在函数checkSolution存在之前就调用了它。因此会抛出错误。

而在snippet2中,var checkSolution func(*TreeNode, int) bool初始化并分配了内存。所以它是存在的,可以被checkSolution的内部函数调用。

我相信这是在这里实现递归的最佳方式。

英文:

I believe that in the snippet1, before assigning value all the statements to the right of := has to be evaluated. Once the evaluation is complete it can be assigned memory.

In the snippet1, we are calling the function checkSolution before it exists. Therefore it throws error.

Whereas in the snippet2 var checkSolution func(*TreeNode, int) bool initializes and assigns memory. So it is in existence, which can be called by the inner function of checkSolution

I believe that this is the best way to achieve the recursion here.

huangapple
  • 本文由 发表于 2021年7月22日 08:45:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/68477918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定