Sunday, February 23, 2014

week #7 Recursion

Recursion is the process of repeating something in terms of itself again and again. We can also use recursive idea in programming language to solve problem by calling functions themselves to solve a smaller problem.

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