Functional programming languages

Source: Internet
Author: User
Tags mathematical functions

The recent period of time always hear or see someone talk about "functional programming", the first contact is about six months ago in a salon, when listening to people speaking, thought it is difficult to understand, functional programming, function, that is C Ah, C + + in the first place is the object, object-oriented programming, C in the first place is not the function, that is functional programming ah, the whole experience of the exchange of a daze, only remember a lambda. But later on the land continued to hear the term, but also know that it is not the way I understand, C is not "functional programming", but "process-oriented programming", is "imperative programming", but it is not very understanding how to distinguish. So the weekend to look at the information, a little "conceptual level" summary.

This article is part of the principles of programming Language (10th edition)

Section references the blog http://www.ruanyifeng.com/blog/2012/04/functional_programming.html?bsh_bid=521023357

Partly taken from their own notes, the provenance is unknown ...

First, the basic knowledge

(a) The classification of programming languages (programming languages)?

Typically divided into 4 categories: imperative, functional, logical, and object-oriented (in fact, there are many different versions of partitioning)

1. Command-type programming language (imperative language)

Computer architecture is an important factor affecting language design, and most of the current languages are designed around the von Neumann structure, which is called "imperative language".

Key features of imperative language:

(1) Variable (storage unit of von Neumann structure)

(2) Assignment statement (data transfer)

(3) Iterative forms of loops (the most efficient form of loops in the von Neumann structure)

2. Functional programming language (functional language)

The imperative programming language is modeled on the architecture of von Neumann, and the purpose of a functional programming language is to simulate mathematical functions as much as possible.

Core features of functional languages:

(1) with "function" as the "variable" in imperative language, functions can be assigned to other variables, either as arguments to other functions, or as return values for other functions.

(2) Do not modify the value of a variable

(3) There is only an expression, no statement. The statement here refers to the absence of a return that is worth some action.

(4) Referential transparency (referential transparency), the function does not depend on the external variable or "state", simply, the same input (parameter), always produces the same output (return value), which is consistent with the characteristics of mathematical functions. Imperative languages cannot do this because of the existence of global variables.

(5) Comparison of imperative languages, recursive forms of loops

3, Logic programming language (Logic programming language) (not studied)

Logical programming Language: a language based on logical symbolic logic, called a logical programming language, or a declarative language

Logic programming: Programming with a symbolic logic as a programming language, often referred to as logical programming

A logical programming language is a rule-based programming language in which a program is a set of facts and rules that represents a program through symbolic logic and uses a logical derivation process to generate results.

4. Object-oriented language (oriented language)

One of the most cordial kind of sounds ...

Strictly speaking, object-oriented language should not be in the state of parallel with the imperative language, the design of object-oriented language is based on the von Neumann structure, it is from the imperative language development, I think it is an imperative language itself.

"Object-oriented": the emphasis is on the development model, and its relative development pattern is "process-oriented"

So the word "imperative language" usually refers to "programming language for process development", such as C; "Object-oriented language" refers to "object-oriented programming language", such as C + + and Java.

Main Features:

(1) Encapsulation: encapsulating data and methods into classes

(2) Inheritance: Subclass inherits Parent class, can automatically share data structure and method of parent class

(3) Polymorphism: Subclasses can modify methods of the parent class

5. Other languages:

(1) scripting language (Scripting language) (python, JavaScript, etc.)

Scripting language is also a kind of language, but the basis of classification is different from the above, the scripting language belongs to the category of "imperative language", the single-handle out of this classification is due to its operating mode-interpretation of execution, no compilation process

(2) Visual language

It is also a subclass of imperative language that can generate snippets of code in a drag-and-drop manner (for example. NET)

(3) Markup Language (Markup Language) (HTML, XML, etc.)

This language does not count as a programming language;

However, in some markup languages (such as HTML and XML) extensions, there are also some programming features--markup and programming mixed language (such as JSTL in the JSP standard tag library)

(4) There are some special-purpose languages, etc.  

(b) How does the language be implemented?

1. Compile and execute--for compiling language

Simply understood, compilation is done by translating the source language (usually the high-level language) into the target language machine language (01 yards) before executing.

SOURCE language--the translation process of the target language, including lexical analysis, grammatical analysis, semantic analysis, multiple steps

What is translation, the language of our daily conversation to think about it, translation is from one language to another language of the process (Chinese-English) (I wrote so much in this, because this simple truth I was after a long time to understand ....) )

2. Fully interpreted execution-for dynamic languages (scripting language)

In contrast to compilation execution, there is no translation process, and the program is interpreted by another program called an interpreter (that is, a virtual machine, also known as an interpreter).

3. Mixed execution--a compromise between compilation and full interpretation

SOURCE Language--(translator)--intermediate language-(interpretation execution)--results

What is the purpose of introducing intermediate languages? It's the language that makes it easier to explain execution.

such as Java:

(1) First, translate the source code into intermediate language (intermediate form)--byte code

(2) Java Virtual machine (bytecode interpreter) interprets byte code execution

It can also be understood that there are two possibilities for compiling:

(1) for compiled languages: Compile is the source code to be able to act on the real machine instructions, the generated language is directly on the hardware

(2) For dynamic language: Compile is to compile the source code into an intermediate form, such as bytecode, bytecode is not the hardware, but the virtual machine

What is a byte code? --is a series of bytes, each byte represents an instruction

(iii) Development environment

The development environment is not difficult to understand, is a tool set used in software development, this toolset can contain only a file system, a text editor, a linker and a compiler, can also contain many integrated tools, each tool can be used through a unified user interface.

Second, function-type programming

So what is functional programming? ..... ..., and ......., ............. let's start by reviewing the basics ...... ..... ..........................

(i) Mathematical functions

A mathematical function is a mapping from one collection to another, which is called a definition field, or a range of domains.

Description of the mapping process: an expression or a table

Characteristics:

(1) The Order of evaluation of mapping expressions is controlled by recursive expressions and conditional expressions

(2) Given the same parameter, always output the same element in the range of several

1, simple function--the function name is followed by a set of parameters in parentheses, followed by an expression, for example:

F (x) =x*x*x, where x is a real number

This function:

(1) Define domain and range are real numbers

(2) If the execution of F (2), that is, the parameter is 2, throughout the evaluation process, the value of x constant is 2, does not change

2. Λ notation-Provides a way to define anonymous functions.

Lambda expression: is an anonymous function that describes the parameters and mappings of the function, however, this function has no name, and the lambda expression, like other function definitions, can have more than one parameter--λ (x) x*x*x

Lambda calculation: A computational model that uses λ-expressions

Lambda Computing is the inspiration for functional programming languages

3. High-order functions--one or more functions as parameters, two types

(1) A function combination, with two function arguments, and a function that has the value of applying the first argument function to the result of a second argument function.

H (x) =f (g (x)); F (x) =x+2; g (x) =3*x;

(2) A single function as a parameter, can be applied to a set of independent variables, the results are grouped into lists or sequences

H (x) =x*x;

A (H, (2,3,4))--(4,9,16)

(ii) Functional Programming Basics

The fundamental point: the purpose of the functional programming language is to simulate mathematical functions as much as possible!

Imperative language: Evaluates an expression, stores the result in a storage unit (a variable in a program), which is the function of an assignment statement

Pure Functional Programming Language:

(1) No variable, no assignment language;

(2) There is no variable, and therefore no iterative result (since the iteration is controlled by the variable), repeated execution must be recursive

(3) Program: Description of function definition and function application

(4) Execution of the program: Evaluation of function application

(5) Given the same parameters, the execution function always produces the same result, which is called referential transparency-making semantics simpler and easier to test than imperative languages

(6) provides a set of functions, a set of functions that make up more complex functions

PS. All of the above are for pure functional languages, but most functional languages now contain some features of imperative language, such as variables and assignment statements;

Early functional languages are usually performed with an interpreter, but most are now compiled and executed.

(iii) Imperative language support for functional programming (Python, for example)

1. Lambda expression--Define anonymous function

Lambda a,b:2*a+b

2. Support higher order functions

(1) function as a parameter pass

1>>>deff (x):2     returnx*x3 4>>> Add ( -5,9, F)51066>>>deff (x):7     returnx*x8 9>>>defAdd (x,y,f):Ten     PrintF (x) +f (Y) One  A      ->>> Add (2,3, F) -13

(2) built-in high-order function map ()-- The map function has two parameters, one is a function, one is a list, and the return value evaluates each parameter .    

1 >>> map (Lambda x:x**3,[1,2,3,4,5])2 [1, 8, +, +]3  def  F (x):4     return x*x*x56 > >> map (f,[1,2,3,4,5])7 [1, 8,8

(3) Built-in high-order function reduce () The--map function has two parameters, one is a function, one is a list, and the return value calls the function repeatedly for each parameter

>>> Reduce (prod, [2, 4, 5, 7,])def  f (x, y)    :return x+y> >> reduce (f,[1,2,3,4,5])15
View Code

(4) Built-in high-order function filter ()--judgment

def f (x):     return x%2==0>>> filter (f,[1,2,3,4,5,6,7,8]) [2, 4, 6, 8]
View Code

(5) .....

3, support closures ....

(iv) Advantages of functional programming

1, for parallel programming-because the variable is not modified, so the process does not need to synchronize mutual exclusion;

2, lazy evaluation: This requires the support of the compiler. An expression is not evaluated immediately after it is bound to a variable, but is evaluated when the value is taken;

3, function certainty: Given the same parameter, there must be an identical output

4, the code is more concise

..........

Other:

1, the imperative language is based on the von Neumann structure, but the functional language is not, so in the current architecture, the advantages of functional language is not obvious, or it is difficult to replace the imperative language

2, pure functional language is now very few, most of the introduction of command-style language features

3, functional language has been more and more attention recently, it is precisely because of the imperative language to join its support

4, understand the functional language, the most important thing is to understand its design as far as possible according to the idea of mathematical functions

Functional programming languages

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.