Functional Programming (1) Understanding of the "Programming Paradigm" and "functions", and the Function Recognition Programming Paradigm

Source: Internet
Author: User
Tags mathematical functions

Functional Programming (1) Understanding of the "Programming Paradigm" and "functions", and the Function Recognition Programming Paradigm

Programming paradigm)

The programming paradigm refers to the idea and perspective of programming to solve the problem. It also determines the programmer's views on program running. Many programming paradigms exist in computer programming, such as imperative programming, Declarative Programming, object-oriented programming, and structured programming. The object-oriented programming paradigm holds that a program is composed of a series of interacting objects, while the structured programming paradigm holds that the program is composed of subprograms, code blocks, for loops, while loops, and other structures. The following describes the imperative programming paradigm and Declarative Programming Paradigm.

1) Imperative programming (Imperative ):

Emphasize program code to simulate the computer running process, and emphasize "what to do first" and "What to do later ". If we want to calculate "2*3 + 1", we should first calculate 2*3 and save it to the temporary variable when writing the code, and then calculate the sum of this temporary variable and 1. Imperative programming is the mainstream programming paradigm. Almost all the code we write belongs to the imperative programming paradigm.

2) Declarative Programming ):

It emphasizes that program code simulates the computing process of the human brain, and emphasizes "what is the end". Compared with imperative programming paradigm, it places more emphasis on results rather than processes. Declarative Programming Paradigm is closer to human thinking, and its thinking level is higher than imperative programming.

Demonstrate the differences between imperative and Declarative Programming paradigms:


Figure 1

Note: not all programming paradigms are opposite. Many paradigms are divided from different perspectives. For example, the object-oriented programming paradigm also belongs to the imperative programming paradigm. Of course, the "imperative programming paradigm" and "Declarative Programming Paradigm" mentioned in this article are opposite.

 

Declarative Programming Paradigm

  Declarative Programming paradigms are common in the following two ways (most common ):

1) Domain Specific Language (DSL ):

The name is unfamiliar, but we often use it. Such as SQL, CSS, and regular expressions. These languages only work in specific fields, and when using these languages, we are mostly writing "statements, declarations. For example, "select * from tb", we only care about the results we want, instead of the specific implementation of the relationship.

2) Functional programming (Functional Program, FP ):

Functional programming is the focus of our discussion. Since it belongs to the Declarative Programming Paradigm, it should also emphasize What rather than How ). Yes, functional programming is different from common imperative programming. It does not care about the specific implementation process of the computer, but focuses only on the problem results.

 

Function programming (Functional Program)):

There are many interpretations of functional programming on the Internet, but most of them are fuzzy abstract. Functional programming on Wikipedia is interpreted as "In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data ", when translated into Chinese, it means"Function programming is a programming paradigm. It regards computer operations as function computing in mathematics and avoids the concept of state and variable.". What does this mean? Many articles explain the features of functional programming, such asFunction is the first citizen","High-order functions (Higher Order Function)","Stateless (No State)","No side effects (Side-Effect)","Easy Concurrent Development"And"Evaluate inertiaAnd so on. However, I think these are just the features or advantages of functional programming, but they do not actually explain the difference between functional programming and general imperative programming. I think to understand functional programming, we must first recognize the concept of "function. Yes, although we think we are familiar with "functions" (or "methods", this article does not distinguish them), are we really familiar with them?

Note: in the future, the blog will introduce the "functional programming" and above features.

 

Programming and mathematical functions:

For the first time, the concept of "function" should be that "y = x + 1" is a straight line in the plane coordinate system when we were in middle school, and later (I don't know which grade) after learning the quadratic function, "y = x ^ 2 + 2 * x + 1" is a parabolic curve in the plane coordinate system. When I was learning functions, I knew the following knowledge points:

1) a function is a ing. After an independent variable is transformed by a ing relationship, the dependent variable (function value) is obtained );

2) each independent variable can, has, and only corresponds to one dependent variable, which is the certainty of the function. That is to say, given an independent variable, the function value is unique at any time;

Then, after going to college to study programming (I started to study programming after I went to college), we met the "function" in the program, and we were very familiar with it. But what is the relationship between it and functions in mathematics? That is to say, are mathematical ideas related to our programming ideas? If we currently write code such as C #, Java, and C ++, there is almost no relationship between them, because the functions in our program can have no parameters ("independent variables" in mathematical functions "), there can also be no return value (dependent variable in a mathematical function). Even if a function has a return value, the results of each operation after the function is called for a given parameter may be different. None of the above can satisfy the concept of mathematical functions. In fact, it is easy to understand that there is almost no relationship between the two functions. mathematics describes the human thinking process, and we (most people currently) the compiled program code describes the computer running process. Communication barriers have long existed between mathematicians and programmers, for example:


Figure 2

As shown in, if the expression "X = X + 1" is mathematical, it is almost impossible for anyone who has never learned programming to look at it, TA will think you are writing wrong. They only recognize "Y = X + 1 ". The reason is very simple. In a program, a symbol can represent a variable, while a variable represents a memory unit. The value in the memory can be overwritten (assigned). In mathematics, A symbol is always a symbol. equal signs "=" indicate the equivalence relationship, and "Y = X + 1" indicate that Y and X + 1 are equivalent, Y is only a replacement symbol of X + 1.

Similarly, functions are the same. A function in mathematics only describes a ing relationship. Given an independent variable, we can get a dependent variable. The functions in the program are more often used as a "function" role that can complete specified tasks. Of course, if a function in a program contains parameters and can return values, it can fully simulate mathematical functions. The following uses C # To compile a delegate, which represents a mona1 function in mathematics:

1 public delegate double Function1X(double x);

As shown in the code above, the delegate signature contains a double type parameter and returns a double type return value. In mathematics, "f (x) = x ^ 2 + 2 * x + 1" can use C # To write the following functions:

1 public double f(double x)
2 {
3      return Math.Pow(x,2) + 2*x + 1;
4 }

Code for calling the value of function f (x) at x = 2: f (2 );. Or use the Lambda expression:

X => Math. Pow (x, 2) + 2 * x + 1;

The function in the program receives a double type parameter. After ing, it returns a double type return value, which corresponds to "f (x) = x ^ 2 + 2 * x + 1. How do binary functions in mathematical functions be expressed in the program? A binary function contains two independent variables. You only need to define two parameters for the function in the program:

1 public delegate double Function2XY(double x,double y);

As shown in the code above, the delegate signature contains two double type parameters and returns a double type return value.

From the above introduction, we can see that if some restrictions are imposed on functions in the program, then it can simulate functions in mathematics:

1) each function must contain input parameters (as independent variables );

2) each function must have a return value (as a dependent variable );

3) at any time, when a function is called with a given parameter, the return value must be consistent.

The third limitation above is to satisfy the "certainty" of the function. This limitation requires that the function in the program cannot depend on external factors during function execution, nor affect the external environment. In other words, it is isolated from the outside world during execution. The functions that meet the preceding conditions are calledPure function (Pure Function)". Pure function interaction has only one channel-input parameters and return values. Pure functions do not read/change global variables or perform I/O operations.


Figure 3

Pure functions are the basis for simulating mathematical functions in program code. Theoretically, in functional programming, functions are the first citizen, and all functions should also belong to "Pure functions ". Here, let's look back at Wikipedia's explanation of functional programming:Function programming is a programming paradigm. It regards computer operations as function computing in mathematics and avoids the concept of state and variable.Obviously, functional programming is moving closer to the mathematical check, using a normal mathematical thinking to solve the problem.

Note: functional programming is based on lambda checksum (Lambda Calculus) and does not fall into the theoretical category of Turing machines. I have not figured out lambda checking, so this article does not mention it in detail. It is not accidental to see Lambda easily reminds us of the Lambda expression introduced in C #3.0. C #3.0 and later began to support "functional programming", which will be discussed later.

(To be continued)

 


Why functional programming is important: Different whiteboard Diagrams

Why should we learn functional programming? For better software design technology! One day, we designed a process to verify the balance of payments. The figure below shows the results we designed several years ago: From then on, we began to learn functional programming. Function programming allows us to think about process processing in the form of data streams: data comes in, transformation, filtering, and Computing. The final result is output. Not only does the software work like this, but each piece of code and every function is modeled into data input and data output. There is no state change in the middle. Thinking about the problem in this way gives us a flowchart like this: Thinking about the program as a data pipeline makes us focus on what the results should be, rather than how each step should be done. This higher level of thinking makes it possible for us to avoid getting stuck in the details of implementation in the system design phase. Regardless of the language used for writing the solution, this way of thinking can give us the following benefits: * functional refinement (the yellow box represents the JIRA task) needs have become very clear in the figure: input and output. Each task can be developed independently. * Each byte can be tested. The Code related to the database has been put out; in addition, each box is completely defined by the input and output. This is the simplest unit component for testing. In this way, functional thinking is good for Agile programming (task decomposition), TDD, and program maintainability. The code is modularized. The problem becomes easier to analyze because we can know the data of each step. Independent components that are easy to test: This is a functional component.

Are functions in programming the same as those in mathematics ?? Related?

Computer functions are interfaces used to facilitate programming, such as api functions.
Of course, you can also create your own computer functions.

Related Article

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.