For week 11, we were asked to revisit one of our previous posts. Since recursion is a large component of the course, and it is probably going to come up a lot in the future, I shall revisit my previous post about recursion. Initially, I mentioned that recursion occurs when a function "calls itself", and that the base case terminates the recursion. While I agree with these statements, my understanding of recursion at that point was very shallow, as we had just started to learn about it. While I agree with my earlier points, I would also like to add to them. I mentioned that recursion looks a lot more concise that using loops (i.e.. for loops). However, as I discovered in A3, recursion isn't always as simple as a single line list comprehension that does the job for you. To elaborate, in my previous post about recursion, I mentioned that I was having trouble with a recursive call in the form of a list comprehension: ie. 1 + max[some_function(x) for x in t.children]. The function that I was referring to was the height(t) function from lab05. It looks like this:
def height(t: Tree):
''' (Tree) -> int
Return 1 + the number of nodes in longest path in Tree t.
'''
if t.children == []:
return 1
else:
return 1 + max([height(x) for x in t.children])
The reason I was having difficulty is due to the fact that I didn't fully understand how the sub-calls get assembled, and where the base cases are combined with the results from the recursive calls. (When I wrote my original post about recursion) Now, I have a better understanding through countless hours of mental breakdowns and practice. Each recursive call goes into a sub-call, until the base case is reached. The results from each call are then propagated back up the tree and combined, in order to create one final, comprehensive result. Each time a recursive call goes into yet another sub call, that function call is "paused", until the result from the sub-call is returned. The computer knows exactly where the recursive call was on hold, and this is why it knows how to assemble everything correctly based on your code and your base-case. I had a really hard time understanding this, but I am more comfortable with it now. Now, back to my argument that recursion isn't always a simple one line expression, I will use A3 as an example. There was a function in A3 called branching_stats, that needed to be implemented for option B. I'm not going to post my code here, because there are people that may not have handed in the assignment yet due to extenuating circumstances (such as myself), but I will speak of it in a general sense. Branching_stats required you to return a dictionary of arity to number of nodes with that arity. I guess you could call it a "dictionarity". HA. HAHAHA. Sorry. Bad pun. Anyways, for this function, not only did I have to implement a recursive call and store it in a variable, that recursive call was in a for-loop. And there were other for loops and a bunch of if and else statements as well. So, this function uses recursion, but it also uses loops as well. At least, that's how I implemented it, vaguely.
Now, to compare my SLOG to other student's SLOGs, I will say this first: I did not see anyone else write about tail-call optimization. So, I'm proud of myself for that, and I definitely stand by what I said. I've chosen to compare my understanding of recursion to 3 other classmates' understanding. The first person I've compared with has the blog domain:
http://beyoncepadthaiismywarriorname.blogspot.ca
We have been partners for all 3 assignments, so I thought it would be interesting to see what we both initially thought of recursion, and how much we've progressed, since I know that we've both been struggling with it. So, I read her post about recursion, and it seems that her understanding was either worse than mine, or she didn't have much to say about recursion at that point. All she said about it was that recursion is when a function calls itself. This is true; I'll give her that! I know she has progressed her understanding from that as well, just like I have. She hasn't written this weeks post yet, so I don't know what she'll have to say about recursion now.
Okay, for the other two SLOGs I'm going to be talking about, I'm not going to lie. I picked them based on the fact that their URLs sounded cool and creative. But they also had insightful things to say that I thought were comparative to my knowledge.
I was happy to see that the blogger behind the following SLOG wrote posts as long as mine:
http://allthemontypythonjokes.blogspot.ca/search?updated-max=2015-03-09T10:13:00-07:00&max-results=7
This person didn't really describe what recursion was, but stated that he/she got better at it with practice in the labs. I felt the same way; the labs were really helpful for understanding recursion. And, he/she also stated that recursion seemed fairly simple, like I initially thought, until he/she realized that he/she would rue those words when the functions got more complicated. The exact same thing happened to me. The blogger also spoke about how tracing recursion helped with his/her understanding. I should have done this more, in retrospect, because I may have understood how sub-calls are assembled sooner. The blogger hasn't written this week's post yet either, so I can't see what his/her understanding is like now, in comparison to my own.
My final comparison is to the following blogger's posts about recursion:
http://gamer-born-programmer.blogspot.ca
Initially, he (I'm using "he" now for simplicity's sake) thought that recursion was very difficult, and had a bare minimum understanding of it. It seems like he had less of an understanding than me. However, he did mention that he had a hard time wrapping his head around how the function calls itself. I had trouble with this too. I understood the concept and how it worked, but I always got stuck on the thought, "What if my function doesn't work? How can it call itself? The function isn't even finished being written yet before it calls itself!" Now I understand this concept. Firstly, you have to trust yourself and assume you've written the function properly, and it returns what it's supposed to. I never trust myself, so I had a lot of trouble doing this. Secondly, even though the function looks like it's not done, it is. The base case stops the recursion and propagates the results back up the tree combinatorially, and the computer knows exactly where each claws paused, and executes it from there. So, in short, trust yourself. In A3, I trusted that my function did what it was supposed to and returned the correct type, so I could store it in a variable and use it later and assemble it with all of the other components returned by the recursive call. I'm staring to be more confident with myself! The blogger of this SLOG, in this week's post, mentions that he used recursion in A3, and he now has a much better grasp on it, which is similar to my experience as well!
That's all for this week- thanks for reading!
No comments:
Post a Comment