I've found recursion to be like a maze. A maze that can trap an individual if it is not traced through to perfection. But as we work through recursion in class I've learned that recursion is a tool that every computer scientist should have equipped in their tool belt. It's capability to turn code from 15 lines of reoccurring for and while loops, and if statements, down to 4 lines of recursing code, just proves that it can change anyones code...IF USED CORRECTLY. I've learned it the hard way, but trying to use your own form of recursion can leave your computer in an infinite loop, forcing you to force quit Wing and risk losing all your code. Hopefully in the upcoming weeks I'll learn how to write my own recursive function and shorten my very own code.
As difficult as recursion is, I have figured out a couple of tricks when tracing through a recursive functions, well through the examples we've been presented with in class. First, I've learned is that most recursive functions start with a conditional statement, Ex: If L is list, or X is str, so by going through that first and seeing what parameters are being passed into the function, sometimes you don't even have to proceed to the recursive part. But sometimes we do have to trace through the recursive part, and when this happens another pattern that I've seen -or maybe that's just how recursive functions are written- is that after the initial condition fails, the proceeding part has a For Loop. When tracing through the for loop, I treat it as if I'm performing the distributive property with the function, onto each element of the iterating portion of the function. By doing so I prevent myself from getting stuck in the maze of a recursive function. Follow these 2 rules, and I know anyone can trace through a recursive function as easy as I do.
fyi: when you go into infinite loop, you can use wing restart (in the options of python shell) rather than quitting wing. You don't lose your code that way.
ReplyDelete