Y combinator realization of javascript

Has done some research to see Liu Peng, the article is not the time did not understand the y combinator before, this seemed to get to know. Ideas are as follows

Section 1


We wanted to write a recursive function F (x): F calls after a series of operations (x-1)
but F in a statement before the completion of F does not exist, we only construct f, f is passed to the call to realize his own as a parameter
we write the f (f, x): a series of operations after calling f (f, x-1)
we find that f (f, here is repeated, if the extracted this function, its function and that write to the F etc. monovalent
redefine f, so that f (f) (x): call f (f) after a series of operations (x-1)
defined over F, contrast, F (x): call F (x-1 after a series of operations )
here, as long as F = f (f), before the F write to write out

f = function(self){return function(n){if(n==1) return 1;else return n*self(self)(n-1)}}
F = f(f)
console.log(F(5))


Observe that f (f) When writing f is defined, if this also replace F to write it?

 

Section 2


Clear, down rewritten
so we pretend F has been in existence, modeled on the front of the f wrote f_gen (F) (x): a series of calls F (x-1) After the operation
we found f_gen (F) = F
Next, we write f. We have such a property of f, f (f) = F, so f_gen (f (f)) = F
if so defined f (f) (x): After a series of operations to call f_gen (f (f)) ( x)
after 100 into the discovery, f (f) (100) = f_gen (f (f)) (100) = 100 * f_gen (f (f)) (99), both F = f_gen (f (f) ) = f (f), F wrote out
here we human flesh realized the Y combinator, the role is to generate F by the f_gen, do essence is to define f_gen this should be written at the time of passing parameters and calls itself f (f) written invoking F, and then the f (f) is part of the transfer to the definition of f.

f_gen = function(Fact){return function(n){if(n==1) return 1;else return n*Fact(n-1)}}
f = function(self){return function(n){return f_gen(self(self))(n)}}
F = f(f)
console.log(F(5))

Burn a little brain, after the contents of clear purely superfluous, clear previously been good enough.

 

Section 3


Y combinator meaning that you can write an anonymous recursive function.
clear the previous program can also write

console.log(function(f){return f(f)}(function(self){return function(n){if(n==1) return 1;else return n*self(self)(n-1)})(6))

Followed by Y, as long as the write Y, where part of the human flesh into code

console.log(function(f){return f(f)}(function(f_gen){return function(self){return function(n){return f_gen(self(self))(n)}}}(function(Fact){return function(n){if(n==1) return 1;else return n*Fact(n-1)}}))(7))

Here, we can use two methods to write the anonymous recursive function

 

end 

 

 Kind of argument is Y operator can identify f (F) = F fixed point F, namely Y (f) = F, this evaluation is a little overrated, because Y is that we deliberately constructed, f (F) is not on F do arithmetic, f (F) (x) is f (F, x) the wording loaded to force it, just to reference itself. Y does not really solve f (x) = x * x is not a fixed point.

 

 

Reproduced in: https: //my.oschina.net/zbaigao/blog/3059015

Guess you like

Origin blog.csdn.net/weixin_34114823/article/details/91926307