nil

This is a page from my book, Functional Programming, Simplified

Recursion: How to Write a ‘sum’ Function in Scala

With all of the images of the previous lesson firmly ingrained in your brain, let’s write a `sum` function using recursion!

Sketching the `sum` function signature

Given a `List` of integers, such as this one:

``val list = List(1, 2, 3, 4)``

let’s start tackling the problem in the usual way, by thinking, “Write the function signature first.”

How to use Lists in Scala match expressions

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.”

Problem

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.

Different ways to create and populate Lists in Scala

This is an excerpt from the Scala Cookbook (partially modified for the internet). This is Recipe 11.1, “Different Ways to Create and Populate a List in Scala”

Problem

You want to create and populate a `List`.

Solution

There are many ways to create and initially populate a `List`: