Recursion
Sum a list of numbers Iterative def sum(L): total = 0 for i in L: total += i return total Recursive def sum(L): if len(L) == 0: return 0 else: return L[0]+ sum(L[1:])
How iteration works def sum(L): total = 0 for i in L: total += i return total sum([4,5,6]) total = 0 i = 4 total = = 4 i = 5 total = = 9 i = 6 total = = 15 15
How recursion works def sum(L): if len(L) == 0: return 0 else: return L[0]+ sum(L[1:] sum([4,5,6]) 4+sum([5,6]) 5+sum([6]) 6+sum([ ]) = = = 15 15
Side by side sum([4,5,6]) total = 0 i = 4 total = = 4 i = 5 total = = 9 i = 6 total = = sum([4,5,6]) 4+sum([5,6]) 5+sum([6]) 6+sum([ ]) = = = 15 15
Fact Iteration and recursion are equivalent. Any iteration can be replaced by recursion. Any recursion can be replaced by iteration.
Why use Recursion? Clarity: The logic of certain task can sometimes be clarified. Elegance: The coding is sometimes more elegant.
A recursive algorithm must… …have a base case. def sum(L): if len(L) == 0: return 0 else: return L[0]+ sum(L[1:]) Empty list [ ] is the base case
A recursive algorithm must… …have a base case. …change its state and move toward the base case. def sum(L): if len(L) == 0: return 0 else: return L[0]+ sum(L[1:]) L L[1:] Closer to [ ]
A recursive algorithm must… …have a base case. …change its state and move toward the base case. …call itself, recursively. def sum(L): if len(L) == 0: return 0 else: return L[0]+ sum(L[1:])
Recursion Worksheet def sum(L): if len(L) == 0: return 0 else: return \ L[0]+sum(L[1:]) a = sum([4,5,6]) Function In: Base In: Base Out: Recursion In: Recursion Out: Function Out: Value Type 1. Input simplified: 2. Input types match: 3. Output types match:
Recursion Worksheet def sum(L): if len(L) == 0: return 0 else: return \ L[0]+sum(L[1:]) a = sum([4,5,6]) Function In: Base In: Base Out: Recursion In: Recursion Out: Function Out: Value Type 1. Input simplified: 2. Input types match: 3. Output types match: [4,5,6] list
Recursion Worksheet def sum(L): if len(L) == 0: return 0 else: return \ L[0]+sum(L[1:]) a = sum([4,5,6]) Function In: Base In: Base Out: Recursion In: Recursion Out: Function Out: Value Type 1. Input simplified: 2. Input types match: 3. Output types match: [4,5,6] list [ ] list
Recursion Worksheet def sum(L): if len(L) == 0: return 0 else: return \ L[0]+sum(L[1:]) a = sum([4,5,6]) Function In: Base In: Base Out: Recursion In: Recursion Out: Function Out: Value Type 1. Input simplified: 2. Input types match: 3. Output types match: [4,5,6] list [ ] list 0 number
Recursion Worksheet def sum(L): if len(L) == 0: return 0 else: return \ L[0]+sum(L[1:]) a = sum([4,5,6]) Function In: Base In: Base Out: Recursion In: Recursion Out: Function Out: Value Type 1. Input simplified: 2. Input types match: 3. Output types match: [4,5,6] list [ ] list 0 number [5,6] list
Recursion Worksheet def sum(L): if len(L) == 0: return 0 else: return \ L[0]+sum(L[1:]) a = sum([4,5,6]) Function In: Base In: Base Out: Recursion In: Recursion Out: Function Out: Value Type 1. Input simplified: 2. Input types match: 3. Output types match: [4,5,6] list [ ] list 0 number [5,6] list 11 number
Recursion Worksheet def sum(L): if len(L) == 0: return 0 else: return \ L[0]+sum(L[1:]) a = sum([4,5,6]) Function In: Base In: Base Out: Recursion In: Recursion Out: Function Out: Value Type 1. Input simplified: 2. Input types match: 3. Output types match: [4,5,6] list [ ] list 0 number [5,6] list 11 number 4+11 number
Recursion Worksheet def sum(L): if len(L) == 0: return 0 else: return \ L[0]+sum(L[1:]) a = sum([4,5,6]) Function In: Base In: Base Out: Recursion In: Recursion Out: Function Out: Value Type 1. Input simplified: 2. Input types match: 3. Output types match: [4,5,6] list [ ] list 0 number [5,6] list 11 number 4+11 number
Recursion Worksheet def sum(L): if len(L) == 0: return 0 else: return \ L[0]+sum(L[1:]) a = sum([4,5,6]) Function In: Base In: Base Out: Recursion In: Recursion Out: Function Out: Value Type 1. Input simplified: 2. Input types match: 3. Output types match: [4,5,6] list [ ] list 0 number [5,6] list 11 number 4+11 number
Recursion Worksheet def sum(L): if len(L) == 0: return 0 else: return \ L[0]+sum(L[1:]) a = sum([4,5,6]) Function In: Base In: Base Out: Recursion In: Recursion Out: Function Out: Value Type 1. Input simplified: 2. Input types match: 3. Output types match: [4,5,6] list [ ] list 0 number [5,6] list 11 number 4+11 number