2.1 basics of function Programming

Source: Internet
Author: User
2.1 basics of function Programming
 
Let's review the first chapter. lambda calculus originated from the mathematical theory of the 1930s S. Today, it is an important part of the theory of computer science. Logically prove and verify the system's auxiliary tools (such as CPU design), or simple programming language, can accurately explain the behavior of other languages.

Note:

In the next section, we will see several examples of "programs" written using lambda calculus. Many concepts can be presented in the purest and cleanest form, which will be seen later in this chapter. In Lambda calculation, the entire "program" is an expression (section 2.2.4). functions can use other functions as parameters (section 2.3.2 ). After discussing the programming language, we will go back to these concepts.
We have included this background material, which can describe some concepts in the purest form. I hope you will find it as interesting as we do, but it is not necessary to understand the rest of the book.

When Alonzo church introduced lambda calculus in 1932, he tried to formalize every mathematical structure and use the most basic mathematical concepts and functions. Write a mathematical function (called f) that adds 10 to any given parameter and can be written:

F (x) = x + 10

Church wants to use functions in all places. In fact, in his formalization, everything is a function. It is impractical to give a name for each function, because when everything is written as a function, many functions may only use it once. Therefore, he introduced a symbol, the name of the function to be written does not need to be:

(λ x. x + 10)

This expression represents a function. It has only one parameter. It is represented by a variable name (x) in Lambda, a Greek letter. A vertex is added after the parameter declaration, add the function body (x + 10 ). In pure lambda calculus, numbers (for example, 10) and mathematical operators (for example, +) are all defined by functions. So there is nothing to remove the functions. This definition is surprising. To make things simple, we will use standard numbers and operators. Let's continue with our example function. If we want to take 32 as the parameter, the result will be:

(λ x. x + 10) 32 = 32 + 10 = 42

Provide parameters for the function (called the application of the function in Lambda calculation) and write the parameters after the function. When using the parameter value to call a function, simply use the parameter value (here 32) to replace the positions of all variables (here X. This is the expression after the first equal sign. If we regard + as a built-in function, it will be called in the next step, resulting in result 42.
Lambda calculus is the most meaningful and cornerstone of the function programming language, that is, any function can take the function as a parameter. This means that we can write a function, take a function (binary operator) and a value as the parameter, use this value as two parameters, and call this binary operator:

(λ op. λ x. (op X ))

As you can see, the function we write uses op and X as parameters. When a function has multiple parameters, use multiple Lambda symbols to declare more parameters. In the lambda function body, op is used to represent the function, and X is used to represent the first and second parameters provided to the OP function. If we use the addition operator as the first parameter and 21 as the second parameter, the code can be written as follows:

(λ op. λ x. (op X) (+) 21 = (λ x. (+) X) 21 = (+) 21 21 = 42

A function with multiple parameters takes the first parameter (OP here) and returns the lambda expression, which is another function. In this way, in the first step, we apply the function (taking OP as the parameter) as the parameter (+), and the result is after the first equal sign, replace the op variable with the plus sign (+ )). This result is still a function with parameters. Therefore, you can continue this process. When applying the function in the next step, 21 is used as the value of parameter X. The result is the expression (+) 21 21, which is another expression of the two numbers addition, which means the same as that of the 21 + 21. Therefore, the final calculation result is 42. As you can see, in lambda calculus, the computation continues until there is no function application that can be computed (the function is followed by its parameters.
Theoretically, the functional thinking comes from lambda computing, but now we need to turn our attention back to the real world. The first group of concepts we will discuss is about Data Representation in function programs, which have a significant impact on program data processing.

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.