The functional paradigm is a strong and synthetic approach to write software. If you are confident with OOP (Object Oriented Programming), it could be difficult to switch to the functional one because most of the structures you are confident with can't be used and are not contemplated. One of them is iteration. When you code with the functional paradigm, you won't use loops described by while, do-while, and for.
The substitute of the iteration is the recursion. This concept is applied to the functions you write. A recursive function is a function that contains a call to itself in its body. Recursion is a powerful method to write compositional code but it's also dangerous to use: you could write infinite loops. Also, if the recursion is not the last operation made in the code, you could reserve too much space in memory. For that last problem, Scala allows you to leverage the tail-recursion.
In this lab, you will understand the principles of recursion and you will use Scala to write recursive functions and tail-recursive functions.
Learning Objectives
Upon completion of this beginner level lab, you will be able to:
- Understand the basic concepts of recursion
- Write recursive functions and tail-recursive functions in Scala
Intended Audience
This lab is intended for:
- Software engineers who need to move to the functional paradigm with Scala
- Developers that want to fully understand how to leverage recursion in Scala
Prerequisites
To get the most out of this lab, you should have basic knowledge of Scala. To achieve this, we suggest taking the following lab:
Updates
April 20th, 2023 - Updated theia to enable autosave
Stefano studies Computer Science and is passionate about technology. He loves working with Cloud services and learning all the best practices for them. Google Cloud Platform and Amazon Web Services are the cloud providers he prefers. He is a Google Cloud Certified Associate Cloud Engineer. Node.js is the programming language he always uses to code. When he's not involved in studying or working, Stefano loves riding his motorbike and exploring new places.