Chapter 6

Recursion for Iteration

Gaining conditionals provided us with a lot of power. We are now able to express logic in our code. Adding functions gave us the ability to break problems into pieces and repeat functionality without retyping code. There is still something very significant that we are missing. Now when we write a piece of logic, it happens once. We can put that logic into a function and then call the function over and over, but it will only happen as many times as we call it. We can not easily vary the number of times that something happens. There is more than one way to make something happen multiple times in Scala. One of these ways, recursion, we can do with just functions and conditionals, constructs that we have already ...

Get Introduction to the Art of Programming Using Scala now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.