# Lists tutorial

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

A list (or array), is a collection of zero o more Fōrmulæ expressions. These expressions can be of any kind.

## Visualization of lists

As you have surely seen in previous tutorials, a list is shown as its elements between curly brackets (or curly braces), separated with commas:

If environments where the direction of writing is right to left (i.e Arabic, Hebrew, etc.), the last list will be shown as (see at right):

Given that elements of list can be of any kind, they can even be lists:

The following is an empty list, a list with no elements:

### Visualization as matrices

If all the elements of a list are also lists having the same number of elements, they are automatically shown as a matrix, then the list of lists `{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}` will be shown as:

## Editing lists

### Creating of a list

The easies way to create a list is pressing the `{` key, it will create a list containing the selected expression as the only element.

However, there are edition to create a list or even a matrix, duplicating the selected expression, see the following table:

Edition Example Editor Result
Conversion to list of single element  or press the `{` key Conversion to list, duplicating the expression    Conversion to matrix, duplicating the expression    ### Inserting elements on a list

In order to insert a new element (a Null expression) on a list, you can do it using the following methods:

Note that the insertion edition is not exclusive to lists, it is an universal edition for any kind of expressions that can accept more subexpressions that it actually has (such as lists). See insertion of expressions in the Edition tutorial.

### Deleting elements of a list

To delete the selected element of an list (it must be a Null expression), you can use the following method:

Note that the deletion edition is not exclusive to lists, it is an universal edition for any kind of expressions that can accept less subexpressions that it actually has (such as lists). See deletion of expressions in the Edition tutorial.

## Operations with lists

### Retrieving elements of lists

Any element of a list is referenced through its positions in the list, it is called index. The first position has the index 1, the second one has the index 2, and so on.

 Note: Several programming languages start numeration of indices as zero, they are called 0-based. Fōrmulæ is 1-based. See zero based numbering.

To retrieve an element, it us used the Child expression, which is visualized as the list and the index in subscript (at the right bottom of the list expression).

See the following examples:

#### Negative indices

Indices can be negative numbers, in such a case, they are counted in reverse order, from the last to the first. The -1 index is the last element, the -2 index is the penultimate element, and so on:

#### Recursive indices

Sometimes, specially when working with matrices, you want to retrieve element of sublists. You can provide a list as the index to indicate the elements you want to traverse.

In the following expression, the index `{3, 2}` means "Get the third element, and then the second one":

Some other examples:

### Cardinality

The operation of retrieving the number of elements of a list is called cardinality. The Cardinality expression in Fōrmulæ has several visualizations, some of them are shown as follows (the first one is the default):

### Appendind and prepending elements

Appending is the operation of adding an element to a list at the end. Prepending is to add the element at the beginning.

In Fōrmulæ, these operations are performed by the Append and Prepend expressions:

### Inserting and deleting elements

To insert an element in any position, you can use the Insert expression. The first parameter is the list, the second one is the element to be inserted, and the third one is the index. Please note that the index reflects the position where the new element will be:

Then if you want to append the element at the end, you should specify an index that the list currently does not have:

To delete an element in any position, you can use the Delete expression. The first parameter is the list and the second one is the index of the element to be deleted:

 Note: The expressions Child, Cardinality, Append, Prepend, Insert and Delete are not exclusive to lists. They are universal expressions that can operate on any kind of expressions having subexpressions (such as lists are), depending of the nature of the target expression. For example, for the Division expression. It can only have two subexpressions. You can use the Child expression to get its numerator (index 1), or its denominator (index 2). The Cardinality expression will always retrieve 2, and the Append, Prepend, Insert and Delete expressions will always produce an error expression.

### Sum of lists

Two (or more) lists can be arithmetically added. All the lists must have the same cardinality. The result is a list with such that cardinality with their elements added:

### Dot product

You can perform a Dot (or scalar) product over two (or more lists). The list must have the same cardinality. Note that the result is not a list:

### Cartesian product

A cartesian product can be performed over two or more lists. The lists can have different cardinalities. It retrieves all the possible combinations of the elements of the lists.

The result is a matrix, having as many columns as number of lists being operated, and as many rows as the product of the cardinalities of their lists.

The following produces all the possible combinations of pairs where the first element is one of the numbers 1 and 2, and the second element is one of the strings "x" and "y". It will produce 2 × 2 = 4 combinations:

In the following example, we have three lists, so the result is triplets. The number or possible combinations are 3 × 2 × 2 = 12.

### Cartesian exponentiation

The cartesian exponentiation is an analogous operation of the arithmetic exponentiation:

In arithmetics, , where is the arithmetic multiplication or product.

In sets (or lists), , where is the cartesian product.

The result is a matrix, having as many columns as the exponent, and as many rows as ce, where c is the cardinality of the list, and e is the exponent.

See the following examples:

### Power set

The power set expression operates on a given list. It retrieves all the possible subsets of the list.

The result is a list of lists that is never shown as a matrix because its sublists are never of the same size (all of them). The number of sublist is 2c, where c is the cardinality of the list.

Note that the list always contains both the original list and the empty list.

### Operations with matrices

In this section, operations with matrices are shown.

As an example, to perform the following:

The expression is (shown purely as lists):

`{{a, b}, {c, d}} + {{1, 2} + {3, 4}}`

Remember that matrices are also lists, so the addition of lists perform the following:

`{{a, b} + {1, 2}}, {{c, d} + {3, 4}}`

It is again an addition of lists, so the following is performed:

`{{a + 1}, {b + 2}}, {{c + 3}, {d + 4}}`

Which it is shown as:

#### Matrix multiplication

Matrix multiplication can be calculated between 2 or more matrices:

#### Matrix transposition

Matrix transposition can also be performed:

#### Matrix determinant

Matrix determinant can be calculated with:

## Arraization

One of the most powerful (and popular) features in Fōrmulæ is arraization. It is about the dynamic construction of lists, that is, the creation of lists by computation.

### Introduction

Most of us know the concept of summation. We can actually perform summations in Fōrmulæ:

Note that the summation produces the expression `0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10` but it is immediately reduced to 55.

See the following example:

Arraization follows the same principle that summation. However, it creates a list instead of a sum. This is the reason of its name.

 Note: The word arraization seems not to be in the dictionary. It was created here to resemble the word summation.

See the previous two examples, now using arraization instead of summation. Note the similarity on both the notation and semantics:

### Form 1. Iterating a fixed number of times

This form is useful to create a list initialized with a fixed value:

To create a list of 10 zeroes:

Now suppose we want a 3 × 5 matrix, initialized to zeros. One possibility is:

But the next one is much better:

### Form 2. Iterating from starting to ending values

This is the form that resembles summation. Let us see some variants and examples.

To create a list of square numbers, from 0 to 10:

However, we would like to see the number and its square:

Note that the result is a matrix.

Let us create a multiplication table:

Example. To create the list of 10 elements, where the first element is the list `{1}`, the second element is the list `{1, 2}`, and so on:

Iterating values do not need to be positive, they can be also negative:

Example. To get the number names, in English and Spanish, for numbers 1 to 10:

Usually, iterating values are stepped by 1. The step value can specified.

The following example creates a list o even values (stepping by 2):

The stepper can be a rational quantity:

The stepper can be also negative:

The following creates a matrix of colors:

The following creates a plot of a few points, combining the width and height of the plot between the values 50, 100, 150 and 200:

### Form 3. Iterating over the elements of a list

The third form of the arraization expression let us create a list by the iteration over the elements of another list. See the following example:

How is formatted 12,345.678 currency units in the different locales? (the resulting table is large, so it was cropped)

The combination of this form of arraization and the cartesian exponentiation can be used to generate truth tables:

The following example generates a truth table for the logical operations of conjunction, disjunction and implication:

Explanation. Remember that the list to iterate on is:

So the symbol x will take these 4 rows (one at a time). But every row is not a single value, it is a list (of two elements). Because of this, the first element is x1 and the second one is x2.

If the elements to iterate on are also lists of known cardinality (such as the last example), we can define a list of symbols instead of only one, in order to these symbols take the values, improving the readability, like the following example, that produces the same result than the last one: