Book Image

Functional Python Programming

By : Steven F. Lott, Steven F. Lott
Book Image

Functional Python Programming

By: Steven F. Lott, Steven F. Lott

Overview of this book

Table of Contents (23 chapters)
Functional Python Programming
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Simple numerical recursions


We can consider all numeric operations to be defined by recursions. For more depth, read about the Peano axioms that define the essential features of numbers. http://en.wikipedia.org/wiki/Peano_axioms is one place to start.

From these axioms, we can see that addition is defined recursively using more primitive notions of the next number, or successor of a number, n, .

To simplify the presentation, we'll assume that we can define a predecessor function,, such that , as long as

Addition between two natural numbers could be defined recursively as follows:

If we use more common and instead of and , we can see that .

This translates neatly in Python, as shown in the following command snippet:

def add(a,b):
    if a == 0: return b
    else: return add(a-1, b+1)

We've simply rearranged common mathematical notation into Python. The if clauses are placed to the left instead of the right.

Generally, we don't provide our own functions in Python to do simple addition. We...