In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to write recursion using Lambda expressions". In daily operation, I believe many people have doubts about how to write recursion using Lambda expressions. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to write recursion using Lambda expressions". Next, please follow the editor to study!
Recursive parameters and return value type assumptions
For common recursive functions, such as factorial and Fibonacci sequence evaluation, we first make the following assumptions:
1. Parameter is of type int (Int32)
two。 The return value is of type long (Int64).
Infer various types based on hypocrisy
FIX g type
According to the description in the previous article:
FIX g = λ n. (ISZERO n) 1 (MULT n ((FIX g) (PRED n) FIX g 55 = 5 * (4 * (3 * (2 * (1 * 1) = 120
FIX g returns the recursive function we need, this recursive function
Receive an int parameter (above assumes Article 1) with a value of 5
Returns a value of type long (section 2 above) 120.
Therefore, it is determined that the type of FIX g can be represented as Func.
At the same time, we can see that n is the parameter of the recursive function, and the type of n is int.
G type
The factorial step function is as follows:
G = λ f. λ n. (ISZERO n) 1 (MULT n (f (PRED n)
C # can be expressed as: G = > f = > n = > n = = 0? 1: n * f (n-1)
Let's infer the type of f:
The parameter of f: F receives n-1 as the parameter, so the type of f parameter is the same as that of n-1, that is, the type of n: int
The return value of f: 1 is returned if it is 0, otherwise n * f (nMel 1) is returned. The return value type of f is the return value type of the entire recursive function, namely long.
It can be determined that the f type is Func.
N = > n = = 0? 1: n * f (n-1) is an incoming int that returns a long of type Func.
Let's first change the representation of g:
Var g = (Func f) = > {Func t = n = > n = = 0? 1: n * f (n-1); return t;}; / / cosmetic code
From the above code, it is clear that g takes a parameter f of type Func and returns a delegate of type Func.
The type of g is Func
FIX Typ
FIX g can be written as FIX (g), and you can see that the type of FIX g = the type of FIX (g) = the type of FIX return value. Knowing that the FIX g type is Func, you can deduce that the FIX return value type is Func.
FIX accepts g as a parameter, and the parameter type of FIX is the type of g. You can see that the parameter type of FIX is Func.
It is concluded that the type of FIX is Func.
(this is probably the most complex generics most developers have ever seen. There are more complicated ones ahead! )
Summary
Name λ calculus expression data type input parameter nint recursive return value FIX g nlong recursive function FIX gFunc one-step function gFunc fixed point combiner FIXFunc
General type
Based on the deduction and summary of the above parts, we can sum up the general types:
Name λ calculus expression data type input parameter nTInput recursive return value FIX g nTResult recursive function FIX gFunc one-step function gFunc fixed point combiner FIXFunc
Based on the type inferred in this article, it is not easy to convert a fixed point combiner to a c # code.
At this point, the study of "how to write recursion using Lambda expressions" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.