Entry no. 3, Week 5:
Topics
1. Impressions on Recursion, and Tracing Recursion (from Week 4)
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