我对这个递归类型定义在这里发生了什么感到有点困惑:
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)
}
任何人都可以阐明在上面的递归类型定义中创建的内容吗?
慕尼黑的夜晚无繁华
相关分类