During the fourth week of lectures, we delved into the perennially dreaded topic of recursion (at least according to most people I know that have ever taken a computer science course). In essence, recursion works by defining something in terms of itself (this may occur multiple times until it is finally reduced to a base case for which a certain value is assigned). Recursion, then, can be distilled to two key elements: a base case for which the recursion 'ends' and an iterative case whereby the function passes a modified version of the argument through itself.
The most conspicuous examples of recursion occur in math, in both the Fibonacci sequence and the use of the factorial. In the case of the Fibonacci sequence, our 'base case' is simply the two numbers that start the sequence, which in this case are a_1 = 1 and a_2 = 1. From here, each successive number is derived by summing the previous two numbers in the sequence. As such, it is easy to see that if you wanted the 4th number in the sequence, you would sum a_1 and a_2 to get a_3 and then sum a_2 and a_3 to get a_4 (in this case, 1 + 1 = 2 = a_3 and 1 + 2 = 3 = a_4). With regards to the factorial, the following code explains recursion quite succinctly.
def factorial(n):
if n == 0 or n == 1:
return 1
else:
result = factorial(n-1) * n
return result
As you can see, the base case occurs for n = 0, 1 which is defined to return 1 (since 0! = 1! = 1) and for all other cases, the function simply calls itself, but on a reduced argument (factorial(n-1) instead of factorial(n)) until n = 0 or n = 1 is reached, at which point the function terminates.
Although many students tend to find recursion a difficult concept to grasp, in reality, our brains are hard-wired to understand recursive thinking. For example, when you say someone is the 'great-great-great-great grandfather of person X', you are speaking recursively. The 'base case' occurs for the word 'grandfather' [0] which we all understand as 'the father of someone's father/mother'. Each successive addition of 'great' triggers a recursive call in which we simply apply 'father of' to our base case 'grandfather'. So, 'great-grandfather' [1] is the 'father of [0]' and 'great-great-grandfather' [2] is the 'father of [1]', etc. So while we may find recursion difficult in a computer science sense, it really is a fundamental part of our everyday language that we rarely question or fail to understand.
As one last example of recursion in speech, we often define people/things in terms of other parameters. These parameters in turn, can be defined recursively as well in terms of other parameters. Although this type of recursion is not as clear as the example above, it serves to illustrate the point that our brains intuitively understand recursive thought. For example, take the following sentence:
'I went to see my mother today, whose car was parked next to a building, that had been recently bought by a millionaire, who owns three dogs.'
While it is a sentence few of us would ever be inclined to say, it's meaning is quite clear. The 'mother' is defined by the 'car next to a building' and the building, in turn, is defined by the face that it 'had been recently bought by a millionaire', and the millionaire, in turn, is defined by the fact that he/she 'owns three dogs'. So while this example is a tad convoluted at best, it elucidates the core principle of recursion as defining something in terms of something else.
Overall, this week's lessons in recursion were enjoyable as well as the lab which I found extremely helpful. Hopefully, recursion will become second nature by the time I am done with CSC148 since it is an extremely important and above all powerful tool in the arsenal of computer scientists.
PS: Hopefully the meaning of the title 'Re-re-re-re-...-cursion' is a bit more clear now.
Wow Jonah! You have a disguised recursion fetish. I should have known...
ReplyDeleteAs for your example with the factorial, I think your code captures recursion very efficiently! Yet you could make it more succinct. Python allows for this:
def factorial(n:int) -> int:
'''Return the factorial of a non-negative integer n. '''
return 1 if (n == 0 or n == 1) else n*factorial(n-1)
In other words, you can do it in one line!
This comment has been removed by the author.
DeleteThanks Zane for your dedicated commitment to reading my blog. As a more experienced CSCer than myself I value your input and expertise on all topics (in CS and all other facets of life). You are right to point out that whenever code can be made more precise it should (just like when doing mathematical proofs, it is always better to strive for 'succinct clarity' than 'convoluted mathematical contortions').
ReplyDelete