Lists

A list is an ordered data structing that contains zero or more expressions, and can be constructed by explicitly listing the expressions separated by commas and surrounded by square brackets, or (equivalently) wrapping square brackets around an expression sequence:

[> [1, 2, 3, 4, 5, 6];

$[1, 2, 3, 4, 5, 6]$

[> bases := (1, t, t^2, t^3, t^4): # An expression sequence
[> [t^(-1), bases, t^5, t^6, t^7];

$\left[\frac{1}{t}, 1, t, t^2, t^3, t^4, t^5, t^6, t^7 \right]$

Lists can be used to store information in order, but should not be used if the entries of that list are to change. A list is unique, and if you change an entry in a list, a new list is created. Thus, this can be very expensive, both computationally and with respect to memory usage.

The empty list is simply []:

$[]$

You can access the entries of a list using an index, and they are indexed from 1 to the number of entries in the list.

[> primes := [2, 3, 5, 7, 11, 13, 17, 19]:
[> primes[1] + primes[5]; # 2 + 11

$13$

To find the number of entries, use the nops(...) function:

[> nops( primes );

$8$

You can also index from the back of the list:

[> primes[-1] + primes[-5]; # last and 5th last entries

$26$

You can extract a sub-list using the range operator:

[> primes[3..-2];

$[7, 11, 13, 17]$

[> primes[3..3];

$[7]$

[> primes[3..]; # same as primes[3..-1]

$[7, 11, 13, 17, 19]$

[> primes[..4]; # same as primes[1..4]

$[2, 3, 5, 7]$

Creating a list

If there is any pattern to the entries of a list, it is often most efficient to generate that list using the seq(...) function. For example,

[> [seq( -10..10, 2 )];

$[-10, -8, -6, -4, -2, 0, 2, 4, 6, 8, 10]$

The seq(...) is executed inside of the kernal, and is therefore very fast and efficient. It can be constructed much more efficiently than with an array or table.

Membership in a list

To determine if an expression is in a list, you need only use the member function:

[> L := [5, 6, 6, 10, 10, 3, 1, 1, 1, 6, 4, 7, 8, 4, 10, 3, 10, 4, 5, 3]:
[> member( 8, L ); # Is '8' in the list?

$true$

[> member( 9, L ); # Is '9' in the list?

$false$

If you want to know at which index the first time a value appears the list, then you can can pass in another symbol, and if the item is found, the symbol is assigned the location:

[> member( 8, L, 'idx' );

$true$

[> idx, L[idx];

$13, 8$

There is a second operator that tests for membership, and that is the in operator:

[> 8 in L; # Is '8' in the list?

$8 \in [5, 6, 6, 10, 10, 3, 1, 1, 1, 6, 4, 7, 8, 4, 10, 3, 10, 4, 5, 3]$

[> 9 in L3; # Is '9' in the list L3?

$9 \in L3$

You will notice that this operator does not immediate evaluate. This is because Maple delays evaluation until a decision is necessary. For example, in the second case, $T$ may not yet have been defined.

If is the condition of a conditional statement, then as soon as the conditional statement, the membership test will be evaluated:

[> x := 42:
[> if x in [1, 3, 5, 7, 9] then
    printf( "%d is a single-digit odd number\n", x );
else
    printf( "%d is not a single-digit odd number\n", x );
end if;
42 is not a single-digit odd number

You can force an evaluation of such a Boolean statement by using the evalb( ... ) function, but this is generally not necessary; it is easier to just use member(...).

Mapping a function onto a list

Suppose you want to apply a function onto the entries of a list. The map(...) function allows this:

[> Digits := 3:
[> xlst := [seq( -2.0..2.0, 0.1 )]:
[> map( sin^2 + 1, xlst );

$[1.23, 1.15, 1.09, 1.04, 1.01, 1., 1.01, 1.04, 1.09, 1.15, 1.23]$

[> map( (x) -> [x, sin(x)], xlst );

This replaces each entry $x$ of the list with a list containing the value $x$ and $\sin(x)$:

$[[-0.5, -0.479], [-0.4, -0.389], [-0.3, -0.296], [-0.2, -0.199], [-0.1, -0.0998],$ $[0., 0.], [0.1, 0.0998], [0.2, 0.199], [0.3, 0.296], [0.4, 0.389], [0.5, 0.479]]$

Fixing a weakness in Maple

In Maple, the mod n operator returns a value between 0 and n - 1, which is uselsss for indexing into arrays. I have suggested decades ago that there should be an equivalent operator mod1 that returns a value between 1 and n, to no avail, so I would strongly encourage you to append this function in your .mapleprofile:

[> mod1 := (m, n) -> ((m - 1) mod n) + 1: