This week we technically only have one lesson on Wednesday,
because Professor Heap spent some time on going through assignment1. On Wednesday, we learned about recursion tree: all the terminologies and
the different method of traversal. Again I found it pretty complicated because
we did not go through any example of tree in class. However, I am assuming we do not
have to worry too much because we have Thanksgiving holiday on next Monday and
term test on next Wednesday. In other words, we probably won't go through any
recursion tree structure until the week of 21st.
In Python, an object can contains some
sort of data and function. It can be assigned as a variable and passed as a
parameter for a function. Objects are usually instance of a class: they are
created by class and they have all the attributes and method that the class
has. In Object-oriented programming, each object and interact with one another:
each object can receive and send information to other objects, as well as
processing the data received. One of the advantage of OOP is that class (object)
are created in hierarchy, so when you have a complicated system with many
different objects, it would be easier to handle. For example, if you want to
add a function to the system, it would be easier in OOP compare to the
traditional model, where program is seen as a list of tasks to perform.
In python and most of other programming
language, recursion is a function that calls upon itself in the definition. It
usually has a base case as well or else it would result in infinite recursion
(which might make the program crash). Recursion breaks down the problem into
sub-problems then combine them together, which in turns simplifies the problem.
Recursion is especially useful when a function is defined based on previous
terms (eg. Fibonacci sequence). Recursion is powerful because we take advantage
of the computer's ability when human brain cannot comprehend to calculate all
the value of the function at once. Recursive algorithm is also easy to read and
write.
Most things that can be written recursively can be rewritten as a loop.
ReplyDeleteIn most situations, a loop is preferable. Each function call, which happens every time you recurse, incurs a performance penalty. A lot of developers now-a-days don't worry about it and its relevance depends on what the software you are writing is doing.
There is also the possibility of blowing out your stack, which is never a good thing. So if stability of your application is a priority, you might want to loop rather than recurse. This is essential if you might end up with data that would cause deep recursion.
Recursive is also harder to debug. Its a big headache because just how deep were you in the recursion when things blew up and how did you get there? Even if you step through the code it's disorienting. Loops are much easier to debug.
Thank you for your comment! I will try to use loop instead of recursion whenever I can (ie. when the assignment and exercise do not specify the use of recursion).
ReplyDelete