## Sequences

A sequence is a generalized list, consisting of zero or more values. You can choose between a number of different kinds of sequence implementations. Scheme traditionally has lists and vectors. Any Java class that implements `java.util.List` is a sequence type. Raw Java arrays can also be viewerd as a sequence, and strings can be viewed a sequence (or vector) of characters. Kawa also provides uniform vectors.

Sequence types differ in their API, but given a sequence type `stype` you can construct instances of that type using the syntax:

    (stype v0 v1 .... vn)

For example:

    (bytevector 9 8 7 6)  ⇒ #u8(9 8 7 6)

For a raw Java class name `jname` you may need to use the empty keyword `||:` to separate constructor parameters (if any) from sequence elements, as in:

    (gnu.lists.U8Vector ||: 9 8 7 6)  ⇒ #u8(9 8 7 6)

This syntax works with any type with a default constructor and a 1-argument `add` method; see Allocating objects for details. You can use the same syntax for allocating arrays, though array creation supports more options.

To extract an element from Scheme sequence of type `stype` there is usually a function ``stype`-ref`. For example:

    (define vec1 (vector 5 6 7 8))
    (vector-ref vec1 2) ⇒ 7

More concisely, you can use (Kawa-specific) function call syntax:

    (vec1 3) ⇒ 8

The index can be another sequence, which creates a new sequence of the selected indexes:

    (vec1 [3 0 2 1]) ⇒ #(8 5 7 6)

It is convenient to use a range to select a sub-sequence:

    (vec1 [1 <=: 3]) ⇒ #(6 7 8)
    (vec1 [2 <:]) ⇒ #(7 8)

The same function call syntax also works for raw Java arrays (though the index is restricted to an integer, not a sequence or array):

    (define arr1 (long[] 4 5 6 7))
    (arr1 3) ⇒ 7

To assign to (replace) an element from a sequence of Scheme type `stype` there is usually a function ``stype`-set!`:

    (vector-set! vec1 1 9)
    vec1 ⇒ #(5 9 7 8)

Again, you can use the function call syntax:

    (set! (vec1 2) 'x)
    vec1 ⇒ #(5 9 x 8)

Procedure: `length` `seq`

> Returns the number of elements of the `seq`.

>     (length '(a b c))             ⇒  3
>     (length '(a (b) (c d e)))     ⇒  3
>     (length '())                  ⇒  0
>     (length [3 4 [] 12])          ⇒  4
>     (length (vector))             ⇒  0
>     (length (int[] 7 6))          ⇒  2

> The length of a string is the number of characters (Unicode code points). In contrast, the `length` method (of the `CharSequence` interface) returns the number of 16-bit code points:

>     (length "Hello")              ⇒  5
>     (define str1 "Hello \x1f603;!")
>     (invoke str1 'length)         ⇒  9
>     (length str1)                 ⇒  8 ; Used to return 9 in Kawa 2.x.
>     (string-length str1)          ⇒  8