Sunday, February 2, 2014

Week 4- Recursion, A1

This week the prof talked about recursion. I think it is a really useful technique and it will definitely help me in the future writing more complex functions. Below I will briefly write about what I learned about recursion, how I feel about it, and some tips I found for myself to improve writing recursion.



WHAT is recursion?
Recursion is a function that calls it self, it is one of the important ideas in computer science.
It sounded a bit weird and didn't make sense to me at first. How and why do we define a function by writing itself in the body?

WHY/WHEN do we use recursion?
We use it because it makes our life easier, like the prof had said in class, "We (computer scientists) avoid doing the same thing repeatedly cause we are lazy." If a problem is too complex and requires too many lines to write, we can break the problems into smaller pieces of itself.

HOW do we write a recursion?
When the prof showed us the examples of recursion during lecture, I felt that I understand what it is. However, when he asked us to try to write a recursion, I was stuck. After reading the slides and examples deeper, I found two important things (for me, at least) that is crucial for coming out with a recursion:

1. I need a base case and general case for when to call the recursion and when to stop. Both cases need to be there for recursion to work. Without base case, the function won't stop and will just keep repeating itself. Without general case, well, then it is not called a recursion anymore.
I think the reason that recursion appeared as such a weird idea for me in the beginning is because I didn't think of the base(stop) case. I thought the program would repeat itself forever without coming out an outcome. After realizing that there's a base case in recursion, it starts to make a lot of sense.

2. Give a good description on recursion! A good description gives us almost exactly the code we will write.
The way for me to write a better description is to practice more and understand what the function is supposed to do.

2014/02/28:
here is a recursion paragraph that I found useful.
--------------------------------------------------------------------

I also started looking at the Assignment 1 (step1-3) this week.
I understood that for the first part, that we need to finish the missing part in TOAHModel, but I'm confused about, for example, what to put in __init__. After leaving out the __init__ first and trying to write the other methods, I realized that I need lists within a list which represent the number of stools and the 'frame'/model.
Also, that I can write some functions using other function (ex: using 'add' in 'fill_first_stool') to make things more easier.

No comments:

Post a Comment