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