Sunday, 8 February 2015

CSC148 - Entry 3

Entry no. 3, Week 5:

Topics

1. Impressions on Recursion, and Tracing Recursion (from Week 4)


Recursion isn't too foreign to myself and some others in this course as we have encountered it before. On my part, I haven't done recursion for a while (2 years give or take a few months) so it was something that I had to get the hang of again. The practice and lab for recursion was straightforward and simple enough for me to get my gears going again. For those who don't have a great grasp of or don't know much about recursion, it is a series of repeated applications of a procedure or definition that re-evaluates until certain criteria are met.

For example of what recursion can look like, below is part of an exercise from a lecture.



At a quick glance, this code accomplishes very much with very little amounts of code. That is, it can sum any size list of ints (nested lists importantly) with 4 lines of code without comments.

What's really key is writing about the criteria needed for the call to start recursion. In this case, if the input L is a list then it calls itself again (that is, it calls sum_list(x) for each element in L) within a List-Comprehension that is later summed. Another key part is that this function returns the input L if the input is not a list. These two parts together will eventually forge and sum a list of Depth-1 thanks to repeated calls on sum_list and List-Comprehension.

No comments:

Post a Comment