Iteration vs. recursion

A few days ago, I showed a fellow burgeoning data scientist my code. What he saw made him gasp in horror. “You use for loops?! You shouldn’t use for loops…,” he said. I was a little surprised. Learning for loops was one of those breakthroughs in coding for me. But I could see what he was saying. I’ve been looking at a lot of Python code recently, and very rarely do I see for loops. I see a lot of defined functions, though. It’s overwhelming to see other programmers doing so much of something that I don’t yet do.

So I took to the Googles to investigate what’s the deal with for loops.

My first search yielded results on how to avoid for loops in R. This first result was interesting: it details how to use ifelse() statements that work on vectors instead of nested if-statements. But I already knew about this statement and it doesn’t always work for what I need to do.

But that result linked to another article that described the apply() functions and how to use it to apply a function across vectors. I’ve used this function once or twice (without really understanding what it did). But what shocked me was that I’ve used its sister function, tapply(), as one of my base functions when taking summary statistics, but I had never realized this is what it was doing. Mind blown. So that’s something I can use in the future.

But I still didn’t get a comfortable answer to why shouldn’t I use for loops and when should I use functions (turns out recursive functions) instead?

Then I read this response that someone gave to this similar question that I am pursuing in which they basically say “Recursive functions are perfect for tree structures. Loops are perfect for iterations and sequences.” I had to look up tree structures, which are basically nested structures with different branches of information. I’ve definitely used for loops and if statements to iterate through a tree structure.

I then started trying to really understand what iterations and sequences are to define use cases for for loops versus recursive functions. I found this useful article, which I liked for how simply it explains the ideas. Recursion relies on a base case (on terminating on a base case), while iteration terminates when the loop-continuation condition fails. It’s a little confusing, because this says that recursion is memory-intensive (which I remember now from a brief little experiment I did with recursive functions a few months ago), but something else I read said using functions instead of for loops reduces memory. I suppose it may depend on the type of function??

Hm, now I’m starting to wonder if iteration does not necessarily mean for loop, since you must be able to have iterative functions?

I guess I’m still confused about when to write functions versus write for loops and if that’s even a reasonable comparison to make.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s