# list

A “Minority Report” Monte Carlo simulation in Scala alvin June 9, 2017 - 8:17am

“The Precogs are never wrong. But occasionally they do disagree.”
~ Minority Report

This article shares the source code for a Monte Carlo simulation that I wrote in Scala. It was inspired by the movie Minority Report, as well as my own experience.

## Background

For the purposes of this simulation, imagine that you have three people that are each “right” roughly 80% of the time. For instance, if they take a test with 100 questions, each of the three individuals will get 80 of the questions right, although they may not get the same questions right or wrong. Given these three people, my question to several statisticians was, “If two of the people have the same answer to a given question, what are the odds that they are correct? Furthermore, if all three of them give the same answer to a question, what are the odds that they are right?”

## Recursion: How Recursive Function Calls Work

An important point to understand about recursive function calls is that just as they “wind up” as they are called repeatedly, they “unwind” rapidly when the function’s end condition is reached.

In the case of the `sum` function, the end condition is reached when the `Nil` element in a `List` is reached. When `sum` gets to the `Nil` element, this pattern of the `match` expression is matched:

## Recursion: How to Write a ‘sum’ Function

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

## Recursion: Let’s Look at Lists

“In computer science, a linked list is a linear collection of data elements, called nodes, each pointing to the next node by means of a pointer.”

## Recursion: Introduction

As you may have noticed from this book’s index, you’re about to jump into a series of lessons on recursive programming. I separated this text into a series of small lessons to make the content easier to read initially, and then easier to refer to later.

## Using Scala Methods As If They Were Functions

“The owls are not what they seem.”

From the television series, Twin Peaks

## Goals

Have you noticed that the Scaladoc for the `List` class `map` method clearly shows that it takes a function?

But despite that, you can somehow pass it a method, and it still works, as shown in this code:

## Scala: How to fill/populate a list (same element or different elements)

As a quick note, if you ever need to fill/populate a Scala list with the same element X number of times, a simple solution is to use the `fill` method, like this:

``````scala> val x = List.fill(3)("foo")
x: List[String] = List(foo, foo, foo)``````

If you want to populate a list with different element values, another approach is to use the `tabulate` method: