递归函数类型定义

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

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

huangapple
  • 本文由 发表于 2022年4月9日 10:08:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/71804759.html
匿名

发表评论

匿名网友

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

确定