Recursion: Loops

    Recursion: Loops

      Breakfast cereal comes in so many shapes, sizes, and flavors. Our favorite ones are

      The ones we love most of all? The cereal made into circles, like Cheerios, Froot Loops, and Apple Jacks. (We know, we know, you're judging us for not picking Lucky Charms and their little freeze-dried marshmallows. Sorry not sorry.)

      Yep, loops are great—both in cereal and in code. Loops might not be fruity or chocolate-y when in computer form, but computer scientists love them anyway.

      The two varieties of code loops are recursive and iterative. Despite not being delicious (but then, what part of the computer is delicious?), they're essential to writing good, efficient code.

      Recursive loops work by calling themselves over and over again—hence the term "loop." Iterative loops, on the other hand, don't rely on calling themselves over and over again to loop. Instead, they make an explicit block of code that jumps back to the beginning each time it ends. If you've ever worked with a for or while loop, you've made an iterative loop.

      Anything that can be done with iterative loops can also be done with recursion.

      We'll let that sink in for a sec.

      No, really. That's actually a thing. It might not be super-efficient both ways all the time, but it's totally possible to convert any iterative loop into recursion and any recursive loop into iteration. Here's an example for you. An iterative function like this walks through a list of numbers using a for loop and adds up all the elements to find the sum.

      FUNCTION sumList(ListOfNumbers):
      	SET sum to 0
      	FOR each number in ListOfNumbers:
      		ADD the integer's value to sum
      	END FOR
      	RETURN sum
      END FUNCTION

      If our list was {3, 5, 8, 12, 11, 1}, then our sum would be 40. It's that simple.

      The length of the array is 6 (since there are 6 elements in the array), meaning that for loop runs six times. We never call sumArray() function from inside sumArray() itself because…we don't need to. In fact, calling it would just over-complicate the code.

      Don't get us wrong: we could write the sumArray() function to be recursive and…we will for comparison's sake but, in this case it's like using a hammer to kill mosquitoes. Sure, you might manage to bang a few out of existence, but the blisters and mosquito bites would prove that this function wouldn't be the best tool for the task. It would just be too much wasted time and effort.

      We warned you.

      FUNCTION sumArrayRecur(listOfNumbers, integerPosition):
      	IF integerPosition is 0
      		RETURN ListOfNumbers[IntegerPosition]
      	ELSE
      		RETURN ListOfNumbers[IntegerPosition] + sumArrayRecur( ListOfNumbers, (IntegerPosition – 1))
      	END IF
      END FUNCTION

      This function still sums up all the elements sequentially from ListOfNumbers[0] to ListOfNumbers[length - 1], but it uses recursion by calling the function from within itself to keep track of the running total.

      Take note, though: we're being very careful about which line of code goes where. To start at ListOfNumbers[0], we have to go backwards so that, as the recursive function works its way out of calling itself, it's going to keep adding the values at each place after index 0.

      The code's also a little brittle (we'll admit it) because whoever calls it needs to know that the original function call needs to make IntegerPosition the index of the last value in the list. Otherwise that function's going to miss any value after IntegerPosition's index.

      Be vewwy, vewwy careful when replacing loops with recursion. You might just end up with more bugs and a slower speed.