Sunday, 1 March 2015

SLOG4: Recursion

As the processing of the course CSC148, we have grasped several essential concepts and skills of coding. Recursion, as the latest but a crucial kind of method for us, is showing how efficient and effective it is to us. There is no doubt that recursion method really presents the strong function of computer programs.

In computer science any object or class or method could apply recursion approach as long as they could treat a set of possible cases somehow towards the base case(s). Therefore, the most important identity of recursion is that it could treat numerous conditions (e.g. every state in a game) with only a few lines of codes. Inside these lines the function itself would be recalled until the final conditions are arrived. The process could be so long but we could throw it into a recursion and let it deal with the situation for us within seconds.

 As I have mentioned in last week's SLOG, a Tree class could exhibit recursive behaviour since there are two basic cases: if there is only one number, then we call it a leaf; else if it contains a number and one another list and the list is non-empty, then we call it a node. For those nodes cases we could always treat it towards leaves via using recursion, which means a function that could call itself while running. Since the basic element of node is the leaf, we could let the recursion function call itself if the variable of the function is a node until every variable we meet is a leaf.

The application of recursion could even be further. During the last two weeks we were concentrating on CSC148 Assignment 2, which includes a strategy called Minimax. It requires us to consider all possible states when a move is applied in a game state. Here we could divide conditions into several cases: win, lose, tie, or the game is not over. For the last one it is a sound choice to use recursion to make the assumption until the game is thought to be (but not actually be) over, and then we could judge whether the player would win or not and therefore decide which move for the current player has more opportunities to win. Since it is impossible for us to list out all the states step by step, recursion is efficient enough for us to make it. For every possible moves we could analyze the first step by ourselves and then apply recursion to "make" (assume to make) next possible moves until the game is over and there is no more possible moves. After that, what we need to do is only returning the max score for the final state of every possible move we analyzed at first. Here we omit doing numerous analyzing manually and give this task to the recursion.

Grasping recursion is also an essential harvest for me. In the past, the only method I could come up with to cope with problem that need to execute some commands more than once was the loop. Yet the definition of loop decides that it could only be applied to solve those simple problems which only need to repeat some identical and simple codes and conditions. Now with knowledge of recursion I am able to tackle more complicated conditions with changing antecedents and variables such as the states of one processing game. I could even say that recursion makes my codes become more intelligent!

The assignment would due in next week and there is not much time for me to complete it. I believe that as I finish it thoroughly I would gain more understanding of recursion and could use it more in the future studying.






No comments:

Post a Comment