In a functional sense, the sum of the multiples of three and five can be defined in two parts:
- The sum of a sequence of numbers
- A sequence of values that pass a simple test condition, for example, being multiples of three and five
The sum of a sequence has a simple, recursive definition:
def sumr(seq): if len(seq) == 0: return 0 return seq[0] + sumr(seq[1:])
We've defined the sum of a sequence in two cases: the base case states that the sum of a zero length sequence is 0, while the recursive case states that the sum of a sequence is the first value plus the sum of the rest of the sequence. Since the recursive definition depends on a shorter sequence, we can be sure that it will (eventually) devolve to the ...