hands-on lab

Start using Recursion with Scala

Difficulty: Intermediate
Duration: Up to 45 minutes
Students: 146
Rating: 3.6/5
Get guided in a real environmentPractice with a step-by-step scenario in a real, provisioned environment.
Learn and validateUse validations to check your solutions every step of the way.
See resultsTrack your knowledge and monitor your progress.

Description

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

Covered topics

Lab steps

Understanding Scala Recursion Principles
Connecting to the Scala Web IDE
Working With Recursion in Scala