Split a list into its first item, items[0], and the rest, items[1:].
- Base case: an empty list;
not items is True.
- Recursive case: handle the first item, then recurse on the shorter rest.
count_items(['apple', 'banana', 'cherry'])
= 1 + count_items(['banana', 'cherry'])
= 1 + (1 + count_items(['cherry']))
= 1 + (1 + (1 + count_items([])))
= 1 + (1 + (1 + 0)) = 3
def count_items(items):
if not items:
return 0
return 1 + count_items(items[1:])
fruits = ['apple', 'banana', 'cherry']
print(count_items(fruits))