Functional programming in lovely Python:python (ii)

Source: Internet
Author: User
Tags range in python

The column continues David's introduction to Functional Programming (FP) in Python. Read this article about the different paradigms for solving programming problems, and in the course of the introduction David will elaborate on several senior FP concepts.

In part 1th, in the previous column on functional programming, I introduced some of the basic concepts of FP. This column will delve further into this highly conceptual area. For our research, Bryn Keller's "Xoltar Toolkit" will provide useful help. Keller Many of FP's advantages in a small module that includes the implementation of pure Python technology. In addition to the functional module, the Xoltar Toolkit includes the lazy module, which supports a structure that assigns values only when needed. Many traditional function languages also have slow assignments, so in these components, the Xoltar Toolkit allows you to get most of the functionality that a functional language like Haskell does.

Binding

Attentive readers may remember the limitations I pointed out in the functional techniques in part 1th. Especially in Python, you cannot avoid a rebind that represents the name of a function expression. In FP, names are often interpreted as abbreviations for longer expressions, but this implies that "the same expression always asks for the same value." This hint is not true if the name of the tag is again bound. For example, let's define some shortcut expressions to use in functional programming, such as:

Listing 1. The following Python FP part of the rebind is going to cause a failure

>>> car =

     lambda

     lst: lst[0]
>>> cdr =

     lambda

     lst: lst[1:]
>>> sum2 =

     lambda

     lst: car(lst)+car(cdr(lst))
>>> sum2(range(10))
1
>>> car =

     lambda

     lst: lst[2]
>>> sum2(range(10))
5

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.