Lambda operator 5b: How of Y

Source: Internet
Author: User
Lambda operator 5b: How of Y is actually a free translation in this article. Some things are saved. Added some private goods. The following post is available. Although y is amazing. Its derivation is not completely beyond the sky. Basically, in order to enable a function without a name to reference itself, we abstracted y step by step. So we know that the derivation process of Y is very meaningful to our programmers: after all, the programming process is also an abstract process. It is good for us to think about how we abstract general rules from the numerous appearances. I used JavaScript to experiment with it. The entire derivation process is like refactoring during programming. We propose a rough solution, and then observe it carefully to find out the abstract places and abstract them. After a more universal result is obtained, repeat the reconstruction steps until the optimal solution is obtained. I guess anyone reading this post knows how to play JavaScript? Besides, a browser has a testing environment. Read the code. Take the factorial function as an example. First, let's look at the common syntax:
1: function fact(n){
2:         if(n == 0){
3:                 return 1;
4:         }
5:
6:         if(n > 0){
7:                 return n * fact(n - 1);
8:         }
9: }
The above JavaScript function defines the internal call itself. The premise for this call is that we use the function name to refer to the function definition. That is to say, FactThe function defined by this name is the above function body. What if we cannot call a function by name (just like lambda operator )? Some old conference may ask: why is this restriction added? Isn't it self-abuse? The reason is simple: Theories need to explore the essence of things. Remember Occam Razor? Without adding entities if necessary. Is the function name a required element or a syntactic sugar? This research method also has practical significance: a complex system is also built on a simple but complete basis. Powerful programming tools are always based on layered abstraction, while the lowest level abstraction layer is always very simple. Simple means thorough, simple means robust. Simplicity means flexibility. Simple means economic. The question is, how simple is it? How can we ensure that the system is not as simple as useless? This is the same as that when a logologist establishes a system, it is always necessary to prove the correctness and completeness of the system. After finding y, we can understand that the binding of the original function name is not the essence. Well, continue. Function FactIs the basic form of recursion. Since we cannot directly call another function through the function name in the function body, we can at least pass the function to be called through parameters. So we get Fact2:
09: fact2 = function(himself, n){
10:         return function(n){
11:                 if(n < 2){
12:                         return 1;
13:                 }
14:
15:                 return n * (something_expression_with_himself);
16:         }
17: }
I use anonymous functions in JavaScript to emphasize the characteristics of Lambda functions that are not named. Fact2 is used here for convenience. The fact2 variable is not involved in the operation. If we call fact2 (fact2, 3) like this, can the fact2 function call itself? This is the self-reference technique mentioned above: add an abstraction layer to pass yourself through parameters. What we need to solve now is what something_expression_with_himeself is. Because both fact2 and himself must call themselves recursively, something_expression_with_himself should start intuitively with himself (himself, n-1. No? The function body and call method remain unchanged, but N becomes n-1. It is consistent with recursion:
19: fact3 = function(himself, n){
20:         if( n < 2 ){
21:                 return 1;
22:         }
23:
24:         return n * himself(himself, n-1);
25: }
Test in your JavaScript Console: fact3 (fact3, 3) does return 6. What, no JavaScript console? Boss, can I use Firefox for online banking? Can programmers who use Firefox not use firebug? Download it now. Download complete? Let's continue. Do you remember that the function in the lambda operator only accepts one parameter? However, fact3 accepts two functions. So we need to scatter a little "Curry ":
17: fact4 = function(himself){
18:         return function(n){
19:                 if( n < 2 ){
20:                         return 1;
21:                 }
22:
23:                 return n * himself(himself)(n-1);
24:         }
25: }
Run 3. You can believe it only when you see it. Fact4 (fact4) (3) = 6, fact4 (fact4) (4) = 24, fact4 (fact4) (5) = 120 .... The problem is that we need to abstract the recursive method of this specific example. Now we need to separate the details irrelevant to self-reference from self-reference itself. Because we are concerned about how to use self-reference to solve recursive problems. We can solve the issue of separating himself and N-related code first. This is because the code for managing N is only related to Factorial, and is not the focus of our attention. It is like separating framework logic and business logic. So we get the fact5 function.
37: fact5 = function(h){
38:         return function(n){
39:                 var f = function(q){
40:                         return function(n){
41:                                 if(n < 2){
42:                                         return 1;
43:                                 }
44:
45:                                 return n * q(n-1);
46:                         }
47:                 }
48:
49:                 return f(h(h))(n);
50:         }
51: }
Run several examples to enhance confidence: fact5 (fact5) (3) = 6, fact5 (fact5) (4) = 24... Note that function f is not actually embedded in fact5. Therefore, we can write it as follows:
37: fucntion f(q){
38:         return function(n){
39:                 if(n < 2){
40:                         return 1;
41:                 }
42:
43:                 return n * q(n-1);
44:         }
45: }
46:
47: function fact5(h){
48:         return function(n){
49:                 return f(h(h))(n);
50:         }
51: }
Now we can see two things: first, the new function. FIt is just a parameterized factorial function-the recursive part is changed to a parameter (also a function) Q. Second, we can further abstract F and strip the specific factorial, so we get Y:
69: function Y(f){
70:          g = function(h){
71:                 return function(x){
72:                         return f(h(h))(x);
73:                 }
74:          }
75:
76:          return g(g);
77: }
Now we can use y to implement factorial:
79: fact6 = Y(
80:         function(h){
81:                 return function(n){
82:                         if(n < 2){
83:                                 return 1;
84:                         }
85:
86:                         return n * h(n-1);
87:                 }
88:         }
89: ); 
Try it, for example, fact6 (3) = 6, fact6 (4) = 24 .... A sigh of relief. After writing for four hours, I finally finished writing about y. Next we can talk about a broader composite operator. It mainly includes three operators: S, K, and I. One abnormal programming language, unlambda, relies on Parsing ski.

 

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.