Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Gauld A.Learning to program (Python)_1.pdf
Скачиваний:
23
Добавлен:
23.08.2013
Размер:
1.34 Mб
Скачать

Functional Programming

In this topic we look at how Python can support yet another programming style: Functional Programming(FP). As with Recursion this is a genuinely advanced topic which you may wish to ignore for the present. Functional techniques do have some uses in day to day programming and the supporters of FP believe it to be a fundamentally better way to develop software.

What is Functional Programming?

Functional programming should not be confused wit imperative (or procedural) programming. Neither is it like object oriented programming. It is something different. No radically so, since the concepts that we will be exploring are familiar programming concepts, just expressed in a different way. The philosophy beghind how these concepts are applied to solving problems is also a little different.

Functional programming is all about expressions. In fact another way to describe FP might be to term it expression oriented programming since in FP everything reduces to an expression. You should recall that an expression is a collection of operations and variables that reults in a single value. Thus x == 5 is a boolean expression. 5 + (7-Y) is an arithmetic expression. And string.search("Hello world", "Hell") is a string expression. The latter is also a function call within the string module and, as we shall see, functions are very important in FP (You might already have guessed that from the name!).

Functions are used as objects in FP. That is they are often passed around within a program in much the same way as other variables. We have seen examples of this in our GUI programs where we assigned the name of a function to the command attribute of a Button control. We treated the event handler function as an object and assigned a reference to the function to the Button. This idea of passing functions around our program is key to FP.

Functional Programs also tend to be heavily List oriented.

Finally FP tries to focus on the what rather than the how of problem solving. That is a functional program should describe the problem to be solved rather than focus on the mechanism of solution. There are several programming languages which aim to work in this way, one of the most widely used is Haskell and the Haskell web site ( www.haskell.org) has numerous papers describing the philosophy of FP as well as the Haskell language. (My personal opinion is that this goal, however laudable, is rather overstated by FP's advocates.)

A pure functional program is structured by defining an expression which captures the intent of the program. Each term of the exression is in turn a statement of a characteristic of the problem (maybe encapsulated as another expression) and the evaluation of each of these terms eventually yields a solution.

Well, that's the theory. Does it work? Yes, sometimes it works very well. For some types of problem it is a natural and powerful technique. Unfortunately for many other problems it requires a fairly abstract thinking style, heavily influenced by mathematical principles. The resultant code is often far from readable to the layman programmer. The resultant code is also very often much shorter than the equivalent imperative code and more reliable. It is these latter qualities that have drawn many conventional imperative or object oriented programmers to investigate FP. Even if not embraced whole heartedly there are several powerful tools that can be used by all.

How does Python do it?

Python provides several functions which enable a functional approach to programming. These functions are all convenience features in that they can be written in Python fairly easily. What is more important however is the intent implicit in their provision, namely to allow the Python programmer to work in a FP manner if he/she wishes.

We will look at some of the the functions provided and see how they operate on some sample data structures that we define as:

94

spam = ['pork','ham','spices'] numbers = [1,2,3,4,5]

def eggs(item): return item

map(aFunction, aSequence)

This function applies a Python function, aFunction to each member of aSequence. The expression:

L = map(eggs, spam) print L

Results in a new list (in this case identical to spam) being returned in L.

We could have done the same thing by writing:

for i in spam: L.append(i)

print L

Notice however, that the map function allows us to remove the need for a nested block of code. From one point of view that reduces the complexity of the program by one level. We'll see that as a recurring theme of FP, that use of the FP functions reduces the relative complexity of the code by eliminating blocks.

filter(aFunction, aSequence)

As the name suggests filter extracts each element in the sequence for which the function returns true. Consider our list of numbers. If we want to create a new list of only odd nuimbers we can produce it like so:

def isOdd(n): return (n%2 != 0) # use mod operator L = filter(isOdd, numbers)

print L

Alternatively we can write:

def isOdd(n): return (n%2 != 0) for i in numbers:

if isOdd(i): l.append(i)

print L

Again notice that the conventional code requires two levels of indentation to achieve the same result. Again the increased indentation is an indication of increased code complexity.

reduce(aFunction, aSequence)

The reduce function is a little less obvious in its intent. This function reduces a list to a single value by combining elements via a supplied function. For example we could sum the values of a list and return the total like this:

def add(i,j): return i+j print reduce(add, numbers)

As before we could do this more conventionally like this:

L = [] # empty list

95