Lists tutorial

From Fōrmulæ wiki
Jump to: navigation, search

This page is a tutorial to manage lists in Fōrmulæ.

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 ListTutorialEdition1 0.png ListTutorialEdition1 1.png or press the { key ListTutorialEdition1 2.png
Conversion to list, duplicating the expression ListTutorialEdition2 0.png ListTutorialEdition2 2.png ListTutorialEdition2 1.png ListTutorialEdition2 3.png
Conversion to matrix, duplicating the expression ListTutorialEdition3 0.png ListTutorialEdition3 2.png ListTutorialEdition3 1.png ListTutorialEdition3 3.png

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:

Example Editor Shortcut keys (Windows/Linux) Shortcut keys (MacOS) Result
ListTutorialInsertion1.png Insert after KeyInsert.png KeyOption.png + KeyRight.png ListTutorialInsertion1 1.png
ListTutorialInsertion1.png Insert before KeyAlt.png + KeyInsert.png KeyOption.png + KeyLeft.png ListTutorialInsertion1 2.png

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:

Example Shortcut key (Windows/Linux) Shortcut key (MacOS) Result
ListTutorialDeletion1.png KeyDelete.png KeyFn.png + KeyBackspace.png ListTutorialDeletion2.png

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.


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:



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):

ListTutorialListCardinality1.png ListTutorialListCardinality2.png ListTutorialListCardinality3.png

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:



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.

Matrix addition

Matrix addition is performed automatically, given the addition of lists.

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:



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.


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.


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:

ListTutorialForm1 1.png

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

ListTutorialForm1 2.png

But the next one is much better:

ListTutorialForm1 3.png

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:

ListTutorialForm2 1.png

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

ListTutorialForm2 2.png

Note that the result is a matrix.

Let us create a multiplication table:

ListTutorialForm2 3.png

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:

ListTutorialForm2 4.png

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

ListTutorialForm2 5.png

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

ListTutorialForm2 6.png

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

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

ListTutorialForm2 7.png

The stepper can be a rational quantity:

ListTutorialForm2 8.png

The stepper can be also negative:

ListTutorialForm2 9.png

The following creates a matrix of colors:

ListTutorialForm2 10.png

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:

ListTutorialForm2 11.png

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:

ListTutorialForm3 1.png

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

ListTutorialForm3 2.png

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:

ListTutorialForm3 3.png

Explanation. Remember that the list to iterate on is:

ListTutorialForm3 3 1.png

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:

ListTutorialForm3 4.png