Arrays and vectors

Like most Lisp dialects, Scheme is based heavily around the list datatype. Around Lisp and most functional programming languages, the word list always refers to a linked list built out of pairs, where each pair contains one list element and a pointer to the next pair.

Also like most Lisp dialects, Scheme has a built-in vector datatype. Whereas a list is built out of out of interlinked but still separate objects, a vector is a single object. The elements of a vector are still separate objects from the vector itself, but the "spine" of the vector that holds the elements together is not divided into parts the way the "spine" of a list is.

Efficiency

The reason for a separate vector datatype is efficiency. Every link in a linked list takes up a little memory. By contrast, a n-element vector is stored internally as an array of n machine words. An additional machine word is not needed for each element to point to the next pair: since a machine word takes up a constant number of bytes in memory, the memory address of the k'th element (using zero-based indexing) is simply k times the machine word size. Hence vector lookup is a constant-time operation, whereas list lookup is O(n).

The advantages of (linked) lists are:

Vectors vs arrays

A vector is a one-dimensional array. Unlike Common Lisp, Scheme does not have a standard multi-dimensional array type. (An m-by-n array can be simulated using a vector with m * n elements.) However, a few Scheme implementations do have a real array datatype.

Standard vector types

Scheme has had a standard vector datatype since R2RS (1985). This type can store any mix of arbitrary Scheme objects as its elements.

Since R6RS (2007) there has also been a standard bytevector datatype. As the name implies, its elements can only be exact integers in the range 0..255. This makes it very fast and compact to handle raw bytes. The standard string->utf8 and utf8->string procedures help convert between strings and bytevectors.

Non-standard numeric vector types

SRFI 4 (Homogeneous numeric vector datatypes) supplies vectors specialized to store a particular type of number.

Integer vectors

The u8vector type is the same as the standard bytevector type. SRFI 4 was written before R6RS, so bytevectors didn’t yet exist.

Floating-point vectors

Complex vectors

SRFI 160 is a backward-compatible update of SRFI 4 that adds:

Fixnum vectors

Chez Scheme provides a fxvector type. It’s a vector that stores fixnum values — exact integers with a range that is the most convenient for the implementation to handle efficiently. Typically this range is the size of a machine word minus a few bits reserved for type tags.

Non-standard array types

SRFI 25 (Multi-dimensional Array Primitives) provides

Types in Scheme implementations

Every implementation below has the standard vector type.