Clojure Study Notes (ii)--functional programming

Source: Internet
Author: User

Defined

"Functional Programming" is a programming paradigm (programming paradigm), that is, how to write a program's methodology. The main idea is to write the operation process as much as possible into a series of nested function calls.

For example, there is now a mathematical expression:

(1 + 2) * 3-4

Traditional procedural programming, which may be written like this:

var a = 1 + 2;

var B = A * 3;

var c = b-4;

Functional programming requires the use of functions, which can be defined as different functions, and then written as follows:

var result = Subtract (multiply (add), 3), 4);

Characteristics

1. function is "Prime Citizen" (first Class)

A function, like any other data type, can be assigned to another variable for equality, as an argument, to another function, or as a return value for another function.

For example, the print variable in the following code is a function that can be used as an argument to another function.

var print = function (i) {console.log (i);};

[1,2,3].foreach (print);

2. Use "expression" only, not "statement"

"Expression" is the process of a simple operation with a return value; "Statement" is an operation that does not return a value. Functional programming requires that only expressions be used and no statements are used. In other words, each step is a simple operation, and there are return values.

The reason is that the development motive of functional programming is to deal with the operation (computation), regardless of the system's read-write (I/O). "Statement" is a read and write operation to the system, so it is excluded.

3. No "Side effects" (Side effect)

"Side effects" refers to interactions within functions (such as modifying the values of global variables), resulting in other results other than operations

Functional programming emphasizes that there is no "side effect", meaning that the function is to remain independent, all functions are to return a new value, no other behavior, especially the value of the external variable must not be modified.

4. Immutable state

Functional programming simply returns the new value without modifying the system variable. Therefore, not modifying the variable is also an important feature of it.

In other types of languages, variables are often used to save "state." The variable is not modified, which means that the state cannot be saved in the traversal. Functional programming uses parameters to save state

Clojure Study Notes (ii)--functional programming

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.