# Folds

## Calculating the sum of all elements of a list

To calculate the sum of terms (of type float, int or big integer) of a number list, it is preferable to use List.sum In other cases, List.fold is the function that is best suited to calculate such a sum.

- Sum of complex numbers

In this example, we declare a list of complex numbers and we calculate the sum of all terms in the list.

At the beginning of the program, add a reference to System.Numerics

open System.Numerics

To calculate the sum, we initialize the accumulator to the complex number 0.

Result:

- Sum of numbers of union type

Suppose that a list be composed of numbers of union (float or int) type and want to calculate the sum of these numbers.

Declare before the following number type:

Calculate the sum of numbers of type number of a list:

Result:

The first parameter of the function, which represents the accumulator is of type float and the second parameter, which represents an item in the list is of type number. But before we add, we need to use a pattern matching and cast to type float when elem is of type Int.

## Intro to folds, with a handful of examples

Folds are (higher-order) functions used with sequences of elements. They collapse `seq<'a>`

into `'b`

where `'b`

is any type (possibly still `'a`

). This is a bit abstract so lets get into concrete practical examples.

# Calculating the sum of all numbers

In this example, `'a`

is an `int`

. We have a list of numbers and we want to calculate sum all the numbers of it. To sum the numbers of the list `[1; 2; 3]`

we write

Let me explain, because we are dealing with lists, we use `fold`

in the `List`

module, hence `List.fold`

. the first argument `fold`

takes is a binary function, the **folder**. The second argument is the **initial value**. `fold`

starts folding the list by consecutively applying the folder function to elements in the list starting with the initial value and the first element. If the list is empty, the inital value is returned!

Schematic overview of an execution example looks like this:

The function `List.sum`

is roughly `List.fold add LanguagePrimitives.GenericZero`

where the generic zero makes it compatible with integers, floats, big integers etc.

# Counting elemets in a list (implementing `count`

)

This is done almost the same as above, but by ignoring the actual value of the element in the list and instead adding 1.

This can also be done like this:

So you could define `count`

as follows:

# Finding the maximum of list

This time we will use `List.reduce`

which is like `List.fold`

but without an initial value as in this case where we don't know what the type is of the values we are compairing:

# Finding the minimum of a list

Just like when finding the max, the folder is different

# Concatenating lists

Here we are taking list of lists
The folder function is the `@`

operator

Or you could use binary operators as your folder function:

# Calculating the factorial of a number

Same idea as when summing the numbers, but now we multiply them. if we want the factorial of `n`

we multiplty all elements in the list `[1 .. n]`

. Code becomes:

# Implementing `forall`

, `exists`

and `contains`

the function `forall`

checks if all elements in a sequence satisfy a condition. `exists`

checks if atleast one element in the list satisfy the condition. First we need to know how to collapse a list of `bool`

values. Well, we use folds ofcourse! boolean operators will be our folder functions.

To check if all elements in a list are `true`

we collapse them with the `&&`

function with `true`

as initial value.

Likewise, to check if one element is `true`

in a list booleans we collapse it with the `||`

operator with `false`

as initial value:

Back to `forall`

and `exists`

. Here we take a list of any type, use the condition to transform all elements to boolean values and then we collapse it down:

To check if all elements in [1; 2; 3; 4] are smaller than 5:

define the `contains`

method with `exists`

:

Or even

Now check if the list [1 .. 5] contains the value 2:

# Implementing `reverse`

:

# Implementing `map`

and `filter`

Is there anything `fold`

can't do? I dont' really know