This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 3.15, “How to use Lists in Scala match expressions.”
You know that a
List data structure is a little different than other collection data structures. It’s built from “cons” cells and ends in a
Nil element. You want to use this to your advantage when working with a
match expression, such as when writing a recursive function.