List assignment, Making lists – Crunch CRiSP File Editor 6 User Manual

Page 23

Advertising
background image

Page 23

The following sections describe various aspects of list management in more detail:

List assignment(pg. 23).

Making lists(pg. 23).

Manipulating list items(pg. 24).

Sorting lists(pg. 24).

Searching Lists(pg. 24).

Informational list primitives(pg. 24).

{button See Also, ALink(crunch,,,)}

List assignment

Assignment may be used to copy one list to another, or to clear out a list (thus freeing its internally allocated
storage). For example,

list lst1;
list lst2 = {1, 2, 3};

lst1 = lst2;

In this example, two lists are defined, the second of which is initialised at the point it is declared. The
statements:

list lst2 = {1, 2, 3};
and list lst2 = quote_list(1, 2, 3);

are equivalent. The curly-brackets initializor get translated by the CRUNCH compiler into the functional form.
The curly-bracket intializor format is more familiar and easier to grasp to C programmers, and is especially
useful when defining lists which contain sublists. For example:

list lst = { 1, 2, 3,
{40, 41, 42},
5};

defines a list with five elements in it, the fourth of which is a sub-list of three elements.

Lists can be built up into long data structures by appending data to them. The memory allocated to a list can
be freed simply by assigning the value NULL to it:

list lst = {1, 2, 3};

lst = NULL;

A null list is one whose length (as returned by the length_of_list primitive is zero). An uninitialised list is
implicitly assigned the value NULL.

{button See Also, ALink(crunch,,,)}

Making Lists

A list can be extended by simply using the binary operator '+'. For example, you can concatenate a single
item to the end of a list or add a new list at the end:

list lst;

lst += 1; /* lst == {1} */
lst += "hello"; /* lst == {1, "hello"} */
lst += lst; /* lst == {1, "hello", 1, "hello"} */

As well as defining lists piece-meal as shown above, lists can be created using two primitives -- quote_list()
and make_list(). quote_list() is a function which takes an arbitrary number of data types and returns a new
list. None of the arguments are evaluated.

Advertising