Refuting "refuted Lao Zhao's" pseudo "recursion"

Source: Internet
Author: User

In the evening, I saw hechongtian's "refuted Lao Zhao's" pseudo "recursion" and probably looked at it. It mainly refuted the concept of "pseudo" recursion proposed by Lao Zhao, especially "pseudo ", everything seems reasonable, but I personally think that there is nothing wrong with Lao Zhao. Lambda looks like a recursive method, and it is not a recursion at all.

I reference the recursive concept of hechongtian:

A method in which a process or function directly or indirectly calls itself in its definition or description.

I think this sentence is quite clear. The popular point is to call yourself. He said recursion should not only be a process or a function, but should include anonymous methods and lambda. I agree that the anonymous method should be regarded as one type, but because it is an anonymous method, we cannot know the method name in development, so we cannot call it, but Lambda (and delegate) isn't it a recursion?

We all know that Lambda is built with a delegate, which only applies to a method. Lambda expressions only construct an anonymous method body and are not executed, loading is delayed only when used, but there is a trap. Lao Zhao previously wrote an article ". net * latency * characteristics of several traps, which are described very clearly. How can we refute Brother Crane? From hisProgramLet's talk about it.Code:

Public static readonlyFunc<Int,Int> FAC = x => x <= 1? 1: x * FAC (X-1 );Static voidMain (String[] ARGs ){IntX = 5;Console. Writeline (FAC (x ));Console. Read ();}

To make it clear, I simply executed it. According to Brother hexiong, FAC should belong to a recursive form when it calls FAC, but do you know what it executes? Look decompilation:

 Internal class  Program { // Fields Public static readonly  Func < Int , Int > FAC; // Methods  Static Program ()  { If (CS $ <> 9 _ cachedanonymousmethoddelegate1 = Null )  { CS $ <> 9 _ cachedanonymousmethoddelegate1 = New  Func < Int , Int > ( Null ,( Intptr ) <. Cctor> B _ 0 );  }  FAC = cs $ <> 9 _ cachedanonymousmethoddelegate1;  }Private Static void Main ( String [] ARGs)  { Console . Writeline (FAC. Invoke (5 )); Console . Read ();  }}

In this way, we can be clear that when we execute the delegate, we will use invoke (ARGs) to call the method body. It is clear that it is the invoke method, not the delegate itself, this has deviated from the concept of recursion.

Let's take a look at our original recursive method:

Static intFAC (IntI ){ReturnI <= 1? 1: I * FAC (I-1 );}

Decompiled code:

 
Private Static intFAC (IntI ){Return(I <= 1 )? 1: (I * FAC (I )));}

You can see it clearly. It calls itself.

Let's continue with the code of brother he, even if he said that the delegate call is a recursion, but there is a "latency feature trap", which has been explained by Lao Zhao, each call is a method body, where parameters are passed in from the external, rather than the method itself, lao Zhao also described "using lambda expressions to write recursive functions". What does it mean? That is, if we change the value of the External Parameter while "recursion" is not over yet when the delegate is called, it will affect the result of "recursion, this is also a trap of closures. Brother he used readonly to make the delegate read-only and wanted to construct a recursive delegate. But what really needs to be bound is not the method body, and he also needs to bind the parameter, your parameter values can be changed externally. In traditional recursion, when the second call is performed, the parameter values are input in the first call and cannot be changed externally, this is the best explanation of the problem.

Summary

It's too late and I don't want to write too much. People I want to understand should understand it. Finally, although the world is changing rapidly, some foundations of programming are still tested. It does not mean that the recursive definition can be changed with anonymous methods and delegation, since its birth, many people have already studied it. Why is it not defined as a commission? There must be some truth in it. Lao Zhao said that it is a pseudo recursion. This is from the code level. Strictly speaking, it is definitely not recursion. I am not a supporter of Lao Zhao. Lao Zhao also said his selfapplicable <t, tresult> is not recursive, so this argument is meaningless.

However, it is better to have a discussion than not to discuss it. I don't know if I am right or not. Let's discuss it with you. You are welcome to make a discussion.

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.