When I asked my friends about what recursion function was, their response was: "You'll have fun" or "Good luck bro." (What a great thing to hear in the breakfast table before class)
I was little intimidated before I went to class but as the lecture went on, I found it not that difficult.
The concept was easy to understand and it made sense to me.
Though, if someone asked me to write a recursion function at that time, I would have had a hard time doing so.
Basically, what recursion function is a function that does not stop running until it meets certain criteria.
In class, list or non-list condition was dealt.
The function returns a number when its parameter is non-list type and counts the length or gets sum of items in the list if the parameter is list type.
Here is an example to guide you to visualize,
def sum_list(L):
"""
I'll skip the docstring
"""
if isinstance(L, list):
return sum([sum_list(x) for x in L])
else: #non-list type (e.g. str, int, and more)
return L
count_list will return the sum of all the items in the list or itself when the parameter is not list type.
sum_list won't stop calculating sum of list until there are no more list type items left in the initial list.
Variation of this would be this:
What would happen if we draw a pointy hill every mid-point of line?
It will look like this picture below. (It is known as Koch fractal)
After couple exercises of writing recursion functions, I am now a bit more comfortable with recursion functions.
For people who wants to understand better in recursion, watch these videos:
1. https://www.youtube.com/watch?v=72hal4Cp_2I
2. https://www.youtube.com/watch?v=s12IdMeVpw0

I remember this koch fractal example from csc165!
ReplyDeleteThis koch fractal example actually helped me understand recursion functions. Nice work!
ReplyDeleteThis koch fractal example actually helped me understand recursion functions. Nice work!
ReplyDelete