C # Implementation of factorial/accumulate Based on Lambda Recursion

Source: Internet
Author: User

I had an interview question the day before yesterday and asked me to accumulate it with C #. I wanted to write it with Lambda. As a result, the interviewer came over in a hurry. It may be the interviewer's lunch time.

Make up for it today. Let's review it. It's time to write it next time.

First, I would like to share a recursive help class. When I write this class, I refer to Lao Zhao and his blog on assembling his head. I would like to express my gratitude here. The implementation idea of this class is very simple and will includeAlgorithmThe description of Lambda is passed and called as a parameter.

The followingCodeThis is the implementation of several help classes. In order to facilitate the release on Weibo, the code is compressed.

 

Compressed code

Using system; Class C {static func <int, int>, Int, int>, func <int, int> Rf = f => X => F (RF (F), x); static func <int, int> r = RF (f, n) => N = 1? 1: N * F (n-1); static void main (string [] ARGs) {console. Write (R (4 );}}

 

Original code 1 Using System;
2
3 Class Program
4 {
5 ///   <Summary>
6 /// A single parameter has a return value Recursive Method generator.
7 ///   </Summary>
8 ///   <Typeparam name = "T"> Single-parameter method parameter type. </Typeparam>
9 ///   <Typeparam name = "tresult"> Type of the method return value. </Typeparam>
10 ///   <Param name = "F"> Recursive operation description method. </Param>
11 ///   <Returns> The generator generates a recursive method. </Returns>
12 Static Func <t, tresult> rfunc <t, tresult> (func <t, tresult>, T, tresult> F)
13 {
14 Return X => F (rfunc (F), X );
15 }
16
17 ///   <Summary>
18 /// Implementation of the factorial method.
19 ///   </Summary>
20 Static Func < Int , Int > Factorial = rfunc < Int , Int > (F, n) => N = 1 ? 1 : N * F (n- 1 ));
21
22 ///   <Summary>
23 ///  ProgramEntry point.
24 ///   </Summary>
25 ///   <Param name = "ARGs"> Command Line Parameter List. </Param>
26 Public Static Void Main ( String [] ARGs)
27 {
28 Console. Write (factorial ( 4 ));
29 }
30 }

In C #, if the method itself is called in the method definition, it is illegal. Unless the static method is used, if there is no such restriction, it can be written as a sentence recursion. I don't know if there is any way for garden friends.

One idea is to use dynamic to dynamically construct a class containing rfunc static methods. However, I do not use dynamic much and I don't know how to add static fields/methods. If I have any idea, I 'd like to give it some advice.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.