# Tail recursion

*A brief intro to tail recursion*

November 04, 2018

Suppose we would like to compute the sum of the elements of a list `List[Int]`

.

Consider the following recursive solution.

```
def sum[A](list: List[A]): Int = {
list match {
case Nil => 0
case x :: xs => x + sum(xs)
}
}
```

The following call to `sum`

will result in a `StackOverflowError`

.

`sum((0 to . . .`

# Currying

*A brief intro to currying*

November 04, 2018

We have the following function.

```
def add(x: Int, y: Int): Int = {
x + y
}
```

with the following signature

```
add: (x: Int, y: Int)Int
```

That is, `add`

is a map from the space of integer 2-tuples to the space of integers.

We say the function has an `arity`

of `n`

or is an `n-ary`

function if it takes `n`

arguments.

# Referential transparency

*Intro to referential transparency*

November 04, 2018

The notion of an *immutable binding* is the notion that a variable can be bound to a value at *most once*.

Consider the following execution block.

```
val a = "Hello"
val b = a.reverse
```

The method `reverse`

does not mutate `a`

. If one calls `a`

at a later point the return value will still be `Hello`

.

One could . . .