Haskell WTF (2) y-conbinator [original]

Source: Internet
Author: User
Tags sin cos

Y-conbinator "Is there any use"? No, in most of the languages that support functional programming, you are free to use recursion, and the goods are scattered in the flesh and blood of functional programming as theoretical cornerstones. This is a math note, which is a math note, which is a math note, and the computer has no relationship with half a dime, Important words to say three times (fled  1.λ-calculus Turing's life opened a lot of brain holes, of which λ-calculus is one of them. (Turing said, to have a world like Turing, and then have Λ-calculus) the important element of λ-calculus is the lambda expression, which can be easily defined in Haskell such as (\x-x+3) It means defining an F (x) =x+3, and , this function is not a name! This is important (the question why in Haskell is \, take the glasses off you will feel that the \ and λ really difficult to distinguish and the latter on the keyboard more difficult to play 233333) Turing's writing is λx.x+3 However, the goods in fact and F (x) =x+3 not much difference .... Lambda expression is not so tall on the argument is anonymous function, that is, for a function it does not have the sin cos func and other names, we are called Red Scarf (λ). To invoke this function is also very simple (λx.x+3 9) equals 12 understanding this and F (9) =12 is not much different  & nbsp;/**********************************************************************************/* mathematically formalized definitions are very useful, It avoids two semantics, while natural language is easy to come by. /* Formal Description: The lambda expression can be defined by a recursive definition/* 1. A variable x is a lambda expression/* 2.t is a lambda expression and x is a variable, then (λx.t) is a lambda expression/* 3. If T S is a lambda expression, then (T s) is also a lambda expression/* The above paragraph is written in the BNF paradigm: (That is, you will find in most Chinese materials)/* <expr>:: = <identifier>/* <expr>:: = (λ<identifier>. <expr>)/* <expr>:: = (<expr> <expr>)/*/* where the 12th sentence defines a function, the third sentence defines a call to ************************** ****************************/  defines two axioms, α conversions, and β-attributions after the lambda expression has been defined, (the word: Alpha conversion is f (x) =x*x and F (y) =y*y is the same, β-attribution is f (x) =x* X then f (5) can be turned into 25 (seemingly stupid idea?) In fact, the amazing number theory you've learned, which is derived from the 5 seemingly stupid definitions of the axiom system of the skin, is actually the lambda expression and two axioms. There is no number in the world (such as a tropical rainforest in the Pirahã language is born without numbers, so that a group of people born without a digital concept 233333) There is no subtraction, the powers, Turing not reconciled, I built the world how can so imperfect! So Turing has moved the numbers of our world to λ. How does the world's skin of the Earth define natural numbers? First we have to have a head s, and then have its successor SS as well as its successor sss,ssss .... Then we artificially make s do 0 ss to do 1 sss to do 2 on it (in fact, the axiom of the skin is a little more complicated than this, some simplification here) Turing assigns λf.λx.x to 0ΛF.ΛX.F X assigned to 1ΛF.ΛX.F (f x) For 3 and so on the fact that we write in the usual way is that X is 0 f (x) is 1 f (f (x)) is 2 ... With the number, you can define subtraction, which is not mentioned here. Turing put these called Church code, plugged into the world of λ, so there is a number and arithmetic, so the above written λx.x+3 is the correct    is a bit like the number of a few? The number of the system in the form of all the propositions and natural numbers do a double-shot, so as to make the latter Proposition G self-reference success, and Turing constructs a set to make it and the natural number of double-shot, the natural number is naturally stuffed into the λ, have to say the master's brain hole is always amazing similarity      2. Recursion has a natural number, the rest of the many things can be defined in the world of Λ-calculus, so Turing all of a sudden, the Boolean number, selection structure, all in the λ world found the corresponding expression method. If, for example, it can be expressed in the world of λ, we know that if in fact there are three elements: an expression, if the expression is a true return value, if the expression is a false return value, then you can construct the lambda expression: let if =λboolean a B. (Boolean and a) or (no T Boolean and B) such as if (3&GT;4) 3 4 = (FALSE and 3) or (true and 4) = False or 4 = 4 Note that the and or not is not a bitwise operation, and in fact Haskell does have a return value, which is similar to a function. Finally, there's a thing that makes him think, recursive notice here that recursion and circulation are actually similar, the tail recursion and the loop can transform each other, Compilers often do this kind of thing. Why can't you plug it in? Give me a chestnut. Now we're going to implement the factorial function, well, the idea of writing in C is this: int fac (int x) {return (x==1)? 1:X*FAC (x-1);} So easy to think of let FAC =λx.if (x==1) 1 (x * FAC x-1) But think about how the modern PC handles recursion? Call yourself. Call is the equivalent of Push + JMP, which pushes the necessary parameters into the stack and then jmp to the address of the calling function. But first .... Call yourself ... Call yourself ... My name is Red scarf (fog) ..... The nature of the anonymous function determines that it is not in any case to call their own, and why can be called if this is just a convenient to write, equivalent to a macro, just for the convenience of writing, but you want to have a function not yet defined what to replace it? So it is never a simple matter to write recursion in the world of λ   3. Fixed point and Y-conbinator----------------------------------------------------------- < to be continued to be free (Keng) >------------------------------------------------  reference:http:// www.zhihu.com/question/20115649https://en.wikipedia.org/wiki/Lambda_calculushttps://en.wikipedia.org/wiki/ Church_encodingbyvoid Functional Programming Notes

Haskell WTF (2) y-conbinator [original]

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.