Now we can look at an example of recursion. Let's say we have a nested number list, a list whose element is either numbers or nested number list. When we are looking for the multiplication of a nested number list: [1, 2, [3, 4], 5], we cannot just simply define a function of multiplication (let's say the function called prod), and write
prod[1, 2, [3, 4], 5]
There will be a TypeError since integer and list cannot be multiplied together directly, so we need to use recursion.
or
No comments:
Post a Comment