有时候需要归递树时,有点麻烦,因为要么把一些要用到的变量声明为全局,要么就得给归递函数写一大堆参数,然后再传递进去。一直想用 利用lambda实现归递,这样,可以省略参数的传递。
Delegate Function fbRecursiveSelf(Of T, TResult)(self As fbRecursiveSelf(Of T, TResult), arg As T) As TResult
Private Sub button_Click(sender As Object, e As RoutedEventArgs) Handles button.Click
Dim selfFac As fbRecursiveSelf(Of Int32, Int32) = Function(f, x)
If x <= 1 Then
Return 1
Else
Return x + f(f, x - 1) 'f(f, x - 1) + f(f, x - 2)
End If
End Function
Console.WriteLine(selfFac(selfFac, 5))
End Sub