英文:
Recursive Function Type Defintions
问题
我对这里的递归类型定义有点困惑:
type Func func() (int, int, Func)
注意:我知道如何通过试错来使用它,但是对于它(递归类型定义)是什么,我非常不确定。
package main
import "fmt"
func fib(x int) int {
if x == 0 {
return 0
} else if x == 1 {
return 1
} else {
return fib(x-1) + fib(x-2)
}
}
type Func func() (int, int, Func)
func get_fib(x int) (int, int, Func) {
return x, fib(x), func() (int, int, Func) { return get_fib(x + 1) }
}
func main() {
d, n, f := get_fib(10)
d1, n1, f1 := f()
d2, n2, _ := f1()
fmt.Println(d, n)
fmt.Println(d1, n1)
fmt.Println(d2, n2)
}
有人能解释一下上面的递归类型定义创建了什么吗?
英文:
I'm a little confused at what is going on here with this recursive type definition:
type Func func() (int, int, Func)
Note: I know how to use it via trial and error but I'm very uncertain as to what it(the recursive type definition) is.
package main
import "fmt"
func fib(x int) int {
if x == 0 {
return 0
} else if x == 1 {
return 1
} else {
return fib(x-1) + fib(x-2)
}
}
type Func func() (int, int, Func)
func get_fib(x int) (int, int, Func) {
return x, fib(x), func() (int, int, Func) { return get_fib(x + 1) }
}
func main() {
d, n, f := get_fib(10)
d1, n1, f1 := f()
d2, n2, _ := f1()
fmt.Println(d, n)
fmt.Println(d1, n1)
fmt.Println(d2, n2)
}
Can anyone shed some light on what's created in the above recursive type definition?
答案1
得分: 2
类型Func func() (int, int, Func)
只是一个函数类型,其类型名称为Func
。您可以将其视为一个没有参数且有3个返回值(最后一个返回值也是Func
类型)的匿名函数。
当将某个变量分配给这个函数类型(Func
)时,该变量将成为该函数的名称。然后,您可以使用变量名作为函数名来调用该函数。
在上面的代码中,d, n, f := get_fib(10)
,d获取到10
,n获取到fib(10)
,f获取到func() (int, int, Func) { return get_fib(11) }
。然后,您可以调用f()
来直接返回get_fib(11)
的结果。
==============================================================
为什么需要递归类型来创建这个功能:
这只是我的想法:get_fib
函数想要返回三个结果:x,fib函数的输入(类型为int),fib函数的结果(类型为int),一个返回get_fib(x+1)
函数的函数(类型为func,而不是Func)。 (因此,get_fib
本身也是一种递归,因为它在返回中使用了get_fib
)。由于函数(类型为func,而不是Func)返回一个get_fib(x+1)
,其返回类型与get_fib(x)
的返回类型相同。因此,函数的返回类型应为int, int, func
(func是get_fib
返回的第三个结果,即函数定义本身)。因此,需要一个递归函数类型。
简而言之:
get_fib
的输出是(int,int,customfunc)customfunc
的输出是(get_fib),即(int,int,customfunc)再次- 因此,
customfunc
的输出是(int,int,customfunc),即递归函数类型
英文:
type Func func() (int, int, Func)
is just a function type whose type name is Func
. You can treat it as an anonymous function with zero parameter and 3 return value (and the last return value is also a Func type).
When assign some variable to this function type (Func
), the variable will be the name of this function. Then you can call the function with variable-name as the fuction name.
In the code above, d, n, f := get_fib(10)
, d get 10
, n get fib(10)
, fu get func() (int, int, Func) { return get_fib(11) }
. Then you can call f()
to which will return the result of get_fib(11)
directly.
==============================================================
Why the recursive type is needed to create this functionality:
Just my thoughts: get_fib function wants to return three results: x, the input of fib function (type int), the result of fib function (type int), a function to return the function of get_fib(x+1) (type func, not Func now). (So get_fib is also a kind of recursive because it uses get_fib in its return.) As the function (type func, not Func now) returns a get_fib(x+1) whose return types are the same as those of get_fib(x). So function's return types should be int, int, func
(func is the third result in get_fib's return which is the function definition itself). So a recursive function type is needed.
In a brief:
- Output of get_fib is (int, int, customfunc)
- Output of customfunc is (get_fib) which is (int, int, customfunc) again.
- So the customfunc's output is (int, int, customfunc) which is a recursive function type
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论