Recursion is useful in solving problems that will need to solve a smaller version of the problem. Recursion is so great that some computer scientist in the past thought that some problem can only be solved with recursion. Usually the recursive code is presented cleaner and easier to read. Even thought the process of coding it is a pain. For example coding the infamous tower of Hanoi or the iteration through a tree created with recursion or creating a tree with recursion, will not need to create extra variables to keep track of it.
Fix:
There was a confusion about recursion that I made at the beginning of the course. I stated that recursion should not use selfdomly:
"Nonetheless recursion is being used only occasionally, because recursion inefficient and it takes more than it needed memory to do it. Also because our processor is not designated to run recursion, complex problem may not be handled, even if the coding is right."
I thought that a recursion would cache out all the memories of the CPU and the RAM, faster than iteration. Because I thought that each time a recursion call itself it would copy the function again into the memory address. Apparently it would just move the address pointer back to the original function code. Moreover in the lecture on map-reduce we learn that modern compiler is smart enough to recognize tail recursion and fix it for us.
I am not sure what you mean by the last paragraph. Do you have a specific example in mind?
ReplyDeleteSorry my mistake, I fix it.
DeleteYes I felt confused too while reading the last paragraph .
ReplyDeleteMy mistake, recursion is not bad to use. In fact thanks to the beauty of OOP, we don't even have to worry about the memory stuff.
Delete