Lists and Arrays

Gamma: Version 4.0
Prev | Chapter 3. Basic Data Types and Mechanisms | Next

# 3.4. Lists and Arrays

An array is an ordered collection of elements, each of which can be of a different type. Each element is associated with a fixed numeric index into the array, and can be set or read using the functions `aset` and `aref`, or through the use of square brackets [ ] . If an attempt is made to set an array at an index beyond the end of the array, the array will increase in size to the given index, filling any undefined intermediate values with `nil`. If an attempt is made to read an element from an array beyond the size of the array, then `aref` will return `nil`, and no error will be reported.

An array is created by a call to `array`, or by reading a Lisp expression of the form `[element element ...]` . A side effect of creating an array using the `[...]` syntax is that the array will be effectively static, in the sense that if the array is defined within a function, then it will not be re-created during each call to the function. It would appear to exist within the code space of the function.

A list is a linked group of consecutive elements called cons cells. A cons cell consists of a reference to the list element and a forward pointer to the next cons cell. This organization necessarily means that a list is single-directional, and also means that any cons cell within the list is the head of another list, consisting of that cons cell and all cells after it in the list. The last cons cell in a list normally has a forward pointer of `nil`.

It is possible to create a list whose last cons cell points to a non-`nil` object. For example, a single cons cell could have a data value of `1`, and a forward pointer to number `2`. This would create a dotted list, written as `(1 . 2)` (note the spaces around the dot). Only the last element in a list can be a dotted cons cell. Thus, it is not possible to create a list `(1 . 2 3)` or `(1 . (2 3))`. A dotted cons cell is created by a call to `cons`, or by reading a Lisp expression of the form `(x . y)`. The form `(x . nil)` is equivalent to `(x)`.

There are two very important functions used to access the members of a list: `car(list)` and `cdr(list)`. The `car` function returns the first element of a list. For example, `car(list(3,2,1))` returns 3. The `cdr` function returns the "tail" of the list, that is, the list without the first element. For example, `cdr(list(3,2,1))` returns (2 1). The combinations of these two functions allow access to any element(s) of a list. For example, `car(cdr(list(3,2,1)))` returns 2, the second element of the list. Normally a shortcut `cadr(list)` is used for `car(cdr(list))`.

For more information see `car, cdr` in the Reference Manual.

A list is created by a call to `cons` or `list`, or by reading a Lisp expression of the form `(x ...)`, or by evaluating a Lisp expression of the form `'(x ...)` or ``(x ...)`.

Both lists and arrays can be traversed using the `with` statement. For example:

        Gamma> sum = 0;
        0
        Gamma> with i in list(1,2,3) do {sum += i;}
        nil
        Gamma> sum;
        6
        Gamma>

For more information on the `with` statement, see `with` in the Reference Manual.

Prev | Home | Next
Strings | Up | Constants

Copyright 1995-2002 by Cogent Real-Time Systems, Inc.