SKB – Scala List pattern matching


This article is part of the Scala knowledge bits Series.

Periodically, I will publish new exercises so you can slowly build up knowledge about Scala.

It is designed to be done in a very short amount of time and learn a little bit each day, just to create a routine.

This episode will teach you about Scala List pattern matching.

Hope you are going to enjoy it! It is designed for anyone to learn Scala from scratch and slowly learn, one Bit at a time.

After this Bit, I would love to hear your feedback in the comments down below.

Feel free to join the Discord server as well if you would like some help and support from the rest of our community.

What are we learning today?

Today we are going to learn about Scala List pattern matching !

Let’s see how List is used in the context of pattern matching.

This is a pre-requirement to talk about recursion later on.

Time to try on the exercise on your own and scroll down for more information when you are done or if you are stuck.


Here is an exercise to complete today.

If I did my job well, you should be able to guess by yourself the solution based on what you previously learned and based on the clues.

But if you get stuck, scroll down to get more information.

The goal of the exercise is to replace the ??? by a piece of code so that the exercise compiles and that’s how you win! Good luck!

You can fill the exercise right in here:

Or, if it does not load, go on to Scastie (i7RNOt1qStGNACeNygqEwQ).

More information about Scala List pattern matching

In this exercise you will learn (or have learned, if you have already solved the puzzle) about Scala List pattern matching.

First we have to talk about how List can be created and modified.

Everything is immutable which is why we always create a new List, we don’t update the previous one.

A few new operator today:

  • Nil: not really an operator, but it describe an empty list, it is a short cut for List.empty[A]
  • ::: to add an element at the start of a List, which is why to create a List using this operator, we have to finish by Nil. We actually prepend element to an empty list.
  • +:: to prepend an element to the List. Same as ::.
  • :+: to append an element at the end of a List

If know that +: and :+ can be confusing but you simply have to remember that : is towards the List.

Pattern matching now

Using the :: we can extract head element(s) from the List.

For instance, we can case head :: tail => to extract the first element of the list. We can also do case a :: b :: tail => to extract the two first element of the list. We can continue with as many as we would like.

Similarly to all the other pattern matching construct, we can add conditions to all those patterns. Either by adding if at the end or by replacing a value name by a literal value, for instance: case 1 :: tail => will match on any list that start by a 1. And case 1 :: Nil => will match on a list containing one element being 1.

Feel free to go back to the exercise, modify the code to try out new things and get a better intuition for Scala List pattern matching.


I hope you have learned something new or had fun during this Scala Knowledge Bit.

Please ask questions or post feedback in the comments below.

Feel free to try on the next Scala Knowledege Bit.

If you are curious about the previous Scala knowledge Bits, go check it out! 🙂

Leave a Reply

%d bloggers like this: