HP Prime/Commands/Guides/All About Lists

From TI-Planet's HP Calcs Wiki
Jump to: navigation, search

The list data type has a type ID of 6.

Making Constants

To make a list, use curly braces:

{}
{1}
{1,2,"str",{(1,2)}}

Lists can have any type in them, and can have nested lists.

Use

List variables are indexable:

LOCAL a := {1,2,3,4};
a[1] := 42;
PRINT(a[3]);

Indexing an element out of range, or giving a non-integer real, throws an error.

Setting an element past the end of a list fills the intermediate elements with zeros:

LOCAL a := {1,2,3,4};
a[8] := 42;

Lists can also be indexed by element 0. This has different semantics between setting and setting index 0. Getting index 0 returns the last item in the list:

LOCAL a := {1,2,3,4};
PRINT(a[0]);

Setting index 0 appends a new item to the list:

LOCAL a := {1,2,3,4};
a[0] := 5;

With nested lists, more than 1 index can be used to get/set members of nested lists:

LOCAL a := {{5,6,7},2,3,4};
PRINT(a[1,2]);
a[1,2] := 42;

The number of indices deep you can index is unlimited.

It is possible to get a sublist by indexing a list variable with a two-element list:

LOCAL a := {1,2,3,4};
PRINT(a[{1,2}]);

An index in this form must be exactly two elements long. The first element has to be a valid index into the list, and the second one must be equal to or larger than the first one. It grabs the elements between indices a and b.

Sublist notation can be combined with multiple indices:

LOCAL a := {{5,6,7},2,3,4};
PRINT(a[1,{1,2}]);

Only the last index into a list variable may be a 2-element list.

Limits

A list is limited to 10,000 elements. Attempting to create a longer list will result in error 38 being thrown.

Bugs

When executing an EXPR string inside a program (not on the command line), you can have no more than 2 indices into a list; it fails with error 22 otherwise.