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 types 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
. For example:
stype
-ref
(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 same function call syntax also works for raw Java arrays:
(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)
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)) ⇒ 2The length of a string is the number of 16-bit code units, not the number the code points. That is the result of the
java.lang.CharSequence#length
method, not thestring-length
procedure.(length "Hello") ⇒ 5 (define str1 "Hello \x1f603;!") (invoke str1 'length) ⇒ 9 (length str1) ⇒ 9 (string-length str1) ⇒ 8