2015年2月1日星期日

Recursive Function

What professor taught to us this week was recursive function, which is brand new for me.  In the lecture, I could master the example that professor provided, sum_list function.  What we need to for the solution of this function just sums sublists and main list up.  However, when I did the exercise in the lab3 handout in the tutorial time, I just lost myself.  I did not know what I should do with that kind of problems.
         Therefore, I asked from help Ta and she just drew a graph and demonstrated to me how the function works steps by steps.  For example, find the solution for nm ([1, 3, 7, 5, 2]).

According to Ta’s method, firstly we need to determine L is a list or not.  In this question, L is a list, therefore, the function will return max([nm(x) for in [1, 3, 7, 5, 2]]).  Next step, let us consider the elements in the list L.  Since all of the elements in list (1, 3, 7, 5 and 2) are not a list.  Therefore, the function will return (1, 3, 7, 5 and 2).  Finally, the function will return max([1, 3, 7, 5, 2]), which is 7.  Thus the solution for this question is 7.

         This approach helps me a lot in solving recursive function.  Through this method, I can find out all the steps that the function works.

没有评论:

发表评论