3.14.1 Sequences
Sequence Constructors in The Racket Guide introduces sequences.
A sequence encapsulates an ordered collection of values. The elements of a sequence can be extracted with one of the for syntactic forms, with the procedures returned by sequence-generate, or by converting the sequence into a stream.
The sequence datatype overlaps with many other datatypes. Among built-in datatypes, the sequence datatype includes the following:
exact nonnegative integers (see below)
strings (see Strings)
byte strings (see Byte Strings)
lists (see Pairs and Lists)
mutable lists (see Mutable Pairs and Lists)
vectors (see Vectors)
hash tables (see Hash Tables)
dictionaries (see Dictionaries)
sets (see Sets)
input ports (see Ports)
streams (see Streams)
An exact number k that is a non-negative integer acts as a sequence similar to (in-range k), except that k by itself is not a stream.
The make-do-sequence function creates a sequence given a thunk that returns procedures to implement a sequence, and the prop:sequence property can be associated with a structure type to implement its implicit conversion to a sequence.
For most sequence types, extracting elements from a sequence has no side-effect on the original sequence value; for example, extracting the sequence of elements from a list does not change the list. For other sequence types, each extraction implies a side effect; for example, extracting the sequence of bytes from a port causes the bytes to be read from the port. A sequence’s state may either span all uses of the sequence, as for a port, or it may be confined to each distinct time that a sequence is initiated by a for form, sequence->stream, sequence-generate, or sequence-generate*. Concretely, the thunk passed to make-do-sequence is called to initiate the sequence each time the sequence is used.
Individual elements of a sequence typically correspond to single values,
but an element may also correspond to multiple values. For example, a
hash table generates two values—
3.14.1.1 Sequence Predicate and Constructors
(in-range end) → stream? |
end : number? |
(in-range start end [step]) → stream? |
start : number? |
end : number? |
step : number? = 1 |
(in-naturals [start]) → stream? |
start : exact-nonnegative-integer? = 0 |
See Pairs and Lists for information on using lists as sequences.
See Mutable Pairs and Lists for information on using mutable lists as sequences.
(in-vector vec [start stop step]) → sequence? |
vec : vector? |
start : exact-nonnegative-integer? = 0 |
stop : (or/c exact-integer? #f) = #f |
step : (and/c exact-integer? (not/c zero?)) = 1 |
See Vectors for information on using vectors as sequences.
The optional arguments start, stop, and step are analogous to in-range, except that a #f value for stop is equivalent to (vector-length vec). That is, the first element in the sequence is (vector-ref vec start), and each successive element is generated by adding step to index of the previous element. The sequence stops before an index that would be greater or equal to end if step is non-negative, or less or equal to end if step is negative.
If start is not a valid index, or stop is not in [-1, (vector-length vec)] then the exn:fail:contract exception is raised. If start is less than stop and step is negative, then the exn:fail:contract:mismatch exception is raised. Similarly, if start is more than stop and step is positive, then the exn:fail:contract:mismatch exception is raised.
An in-vector application can provide better performance for vector iteration when it appears directly in a for clause.
(in-string str [start stop step]) → sequence? |
str : string? |
start : exact-nonnegative-integer? = 0 |
stop : (or/c exact-integer? #f) = #f |
step : (and/c exact-integer? (not/c zero?)) = 1 |
See Strings for information on using strings as sequences.
The optional arguments start, stop, and step are as in in-vector.
An in-string application can provide better performance for string iteration when it appears directly in a for clause.
(in-bytes bstr [start stop step]) → sequence? |
bstr : bytes? |
start : exact-nonnegative-integer? = 0 |
stop : (or/c exact-integer? #f) = #f |
step : (and/c exact-integer? (not/c zero?)) = 1 |
See Byte Strings for information on using byte strings as sequences.
The optional arguments start, stop, and step are as in in-vector.
An in-bytes application can provide better performance for byte string iteration when it appears directly in a for clause.
(in-port [r in]) → sequence? |
r : (input-port? . -> . any/c) = read |
in : input-port? = (current-input-port) |
(in-input-port-bytes in) → sequence? |
in : input-port? |
(in-input-port-chars in) → sequence? |
in : input-port? |
(in-lines [in mode]) → sequence? | ||||||||||||
in : input-port? = (current-input-port) | ||||||||||||
|
(in-bytes-lines [in mode]) → sequence? | ||||||||||||
in : input-port? = (current-input-port) | ||||||||||||
|
Examples: | |||||||
|
See Hash Tables for information on using hash tables as sequences.
(in-hash-keys hash) → sequence? |
hash : hash? |
Examples: | |||||||
|
(in-hash-values hash) → sequence? |
hash : hash? |
Examples: | |||||||
|
(in-hash-pairs hash) → sequence? |
hash : hash? |
Examples: | |||||||
|
(in-directory [dir]) → sequence? |
dir : (or/c #f path-string?) = #f |
(in-producer producer stop args ...) → sequence? |
producer : procedure? |
stop : any/c |
args : any/c |
(in-indexed seq) → sequence? |
seq : sequence? |
(in-sequences seq ...) → sequence? |
seq : sequence? |
(in-parallel seq ...) → sequence? |
seq : sequence? |
(in-values-sequence seq) → sequence? |
seq : sequence? |
(in-values*-sequence seq) → sequence? |
seq : sequence? |
(make-do-sequence thunk) → sequence? | ||||||||||||
|
The first result is a pos->element procedure that takes the current position and returns the value(s) for the current element.
The second result is a next-pos procedure that takes the current position and returns the next position.
The third result is the initial position.
The fourth result is a continue-with-pos? function that takes the current position and returns a true result if the sequence includes the value(s) for the current position, and false if the sequence should end instead of including the value(s). Alternatively, the fourth result can be #f to indicate that the sequence should always include the current value(s).
The fifth result is a continue-with-val? function that is like the fourth result, but it takes the current element value(s) instead of the current position. Alternatively, the fifth result can be #f to indicate that the sequence should always include the value(s) at the current position.
The sixth result is a continue-after-pos+val? procedure that takes both the current position and the current element value(s) and determines whether the sequence ends after the current element is already included in the sequence. Alternatively, the sixth result can be #f to indicate that the sequence can always continue after the current value(s).
Each of the procedures listed above is called only once per position. Among the last three procedures, as soon as one of the procedures returns #f, the sequence ends, and none are called again. Typically, one of the functions determines the end condition, and #f is used in place of the other two functions.
Examples: | ||||||||||||||||||
|
3.14.1.2 Sequence Conversion
(sequence->stream seq) → stream? |
seq : sequence? |
In extracting an element from seq involves a side-effect, then the effect is performed each time that either stream-first or stream-rest is first used to access or skip an element.
(sequence-generate* seq) | ||||||
| ||||||
seq : sequence? |
3.14.1.3 Sequence Combinations
(sequence->list s) → list? |
s : sequence? |
(sequence-ref s i) → any |
s : sequence? |
i : exact-nonnegative-integer? |
(sequence-tail s i) → sequence? |
s : sequence? |
i : exact-nonnegative-integer? |
In case initiating s involves a side effect, the sequence s is not initiated until the resulting sequence is initiated, at which point the first i elements are extracted from the sequence.
(sequence-append s ...) → sequence? |
s : sequence? |
If all given ss are streams, the result is also a stream.
(sequence-map f s) → sequence? |
f : procedure? |
s : sequence? |
If s is a stream, then the result is also a stream.
(sequence-count f s) → exact-nonnegative-integer? |
f : procedure? |
s : sequence? |
If s is a stream, then the result is also a stream.
(sequence-add-between s e) → sequence? |
s : sequence? |
e : any/c |