-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating

Functional Python Programming

We can consider all numeric operations to be defined by recursions. For more details, read about the Peano axioms that define the essential features of numbers at: http://en.wikipedia.org/wiki/Peano_axioms.
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
. This formalizes the idea that a number is the successor of the number's predecessor.
Addition between two natural numbers could be defined recursively as follows:
If we use the more common
and
instead of
and
, we can see that
.
This translates neatly into Python, as shown in the following command snippet:
def add(a: int, b: int) -> int: if a == 0: return b else: return add(a-1, b+1)
We've rearranged common mathematical notation into Python.
There's no good reason to provide our...
Change the font size
Change margin width
Change background colour