Functional programming in lovely Python:python (i)

Source: Internet
Author: User
Tags error code in python

Although the user often sees Python as a procedural and object-oriented language, it actually contains everything needed to implement full function programming. This paper discusses the general concept of functional programming, and illustrates the method of implementing function technology in Python.

We'd better start with the hardest question: "What exactly is functional programming (FP)?" "One answer might say that FP is what you do when you're programming with Lisp, Scheme, Haskell, ML, OCAML, clean, Mercury, Erlang (or some other) language." This is a sound answer, but it is not a precise explanation of the problem. Unfortunately, even functional programmers themselves have a hard time understanding what FP really is. The story of "elephant" is used to describe the situation as appropriate. You can also safely compare FP with command programming (using, for example, the operations performed in C, Pascal, C + +, Java, Perl, Awk, TCL, and most other languages, at least to a large extent).

Personally, I'll sketch functional programming as having at least one of the following characteristics. A functional language makes these things simple and makes other things difficult or impossible:

The function is the first Class (object). That is, every operation that can be done with the data can be done using the function itself (for example, passing a function to another function).

Use recursion as the primary control structure. In some languages, no other "looping" constructs exist.

Focus on listing list processing (for example, name Lisp). Lists are often used in conjunction with the recursion of the child list to replace the loop.

The "pure" function language can avoid side effects. This is not included in the most common pattern in the command language, specifying the first, and then assigning another value to the same variable to keep track of the program state.

FP discourages or does not allow statements at all, instead using expression evaluation (in other words, functions plus arguments). In a very pure case, a program is an expression (plus a supported definition).

FP is concerned with what is calculated rather than how it is calculated.

Many FP take advantage of the "higher" function (in other words, functions operate on some functions, and these functions operate on other functions).

The advocates of functional programming believe that all of these features lead to faster development of shorter and less error code. Moreover, senior theorists in the fields of computer science, logic and mathematics have found that the formal performance of functional languages and programs is much easier than command languages and programs.

Inherent Python function capabilities

Python has most of the FP features listed above since Python 1.0. But for most Python features, they are rendered in a very mixed language. In large part because of the OOP features of Python, you can use the parts you want to use and ignore the rest (until you need it later). Using Python 2.0, a list of connotations adds some great "syntactic whitewash". Although the list content does not add any new capabilities, they make many old abilities look a lot better.

The basic elements of FP in Python are function map (), reduce () and filter (), and operator Lambda. In Python 1.x, the Apply () function is also handy for applying the return value of a function directly to another function. Python 2.0 provides an improved syntax for this purpose. It may be surprising, but few of these functions (and basic operators) are nearly enough to write any Python program; In particular, all flow control statements (if, elif, else, assert, try, except, finally, for, break , continue, while, Def) can be handled in a functional style using only FP functions and operators. While virtually eliminating all flow control commands in a program may be useful only for adding "chaotic Python" competition (as with code that looks very much like Lisp), it is worthwhile to understand how FP uses functions and recursion to represent flow control.

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.