Functional Programming in Python

Introduction

The difficulty of reasoning about the behaviour of stateful programs, especially in concurrnent enviroments, has led to increased in intrest in a programming paradigm called functional programming. This style emphasises the connection between programs and mathematics, encouraging code that is easy to understand and, in some critical cases, even possible to prove properties of.

First Class Functions

One of the first and most important features of functional programming is first class functions. This means that a function can be treated as a value by the programming language. This enables the use of higher order functions, or functors: functions that take functions amoungst their arguments. The classic example of this is the map function.

def map(func, iterable):
    results = [func(value) for value in iterable]

Purity

Functional programs heavily stress the control of side effects. A side effect can be thought of as any modification to the state of the program (or the enviroment it runs in). As a function with no side effects at all is obviously of limited usefulness (being unable to even print to the screen), functional programs do not eliminate side effects but rather capture them in language abstraction so that their effect on the running of the program is easy to reason about.

def pure(x)
    y = x.value + 1  # the value pointed to by x has not changed
    return y

def impure(y):
    y.value = y.value + 1  # The value pointed to by y has now changed throughout the whole program!
    return y

Recursion

Because traditional iteration, such as:

for x in [1, 2, 3]:
    print(x)

is inherently stateful (in this case the state variable is x as it takes different variable in different parts of the loop), functional programs often prefer recursion.

def printx(range):
    if len(x) >= 1:
        print(x[0])
    else:
        printx(x[1:])
    return 
printx([1,2,3])

Many algorithms are much more naturally expressed in terms of recursive function calls than as imperative style loops. This style of writing iteration has close links the mathematical proof by induction, which can often be used to reason about the behaviour of such functions.

Referential Transparency

One of the most attractive features of functional programming is referential transparency. Functional programs do not feature assignment, which means that the value of some identifier never changes. This means that all expressions in the program are trivally reducible. This is the key feature of functional programing that contrast.

# Imperative
x = 1  # assignment
x = 2  # REASIGNMENT

# Functional (Python does not feature definition so we borrow the syntax of Haskell)
x := 1  # definition
x := 2 (gramatically incorrect in a functional language, and a deviation from the )

Conclusion

So, would a functional style be helpful for your coding needs? Is the abstraction worth the penalty to performance? Or do you think the inneficiancy born of a refusal to exploit the inherent statefulness of hardware is a waste?

Author