1 Language Model
2 Syntactic Forms
3 Datatypes
4 Structures
5 Classes and Objects
6 Units
7 Contracts
8 Pattern Matching
9 Control Flow
10 Concurrency
11 Macros
12 Input and Output
13 Reflection and Security
14 Operating System
15 Memory Management
16 Running PLT Scheme
Bibliography
Index
On this page:
3.14.1 Sequence Predicate and Constructors
sequence?
in-range
in-naturals
in-list
in-vector
in-string
in-bytes
in-input-port-bytes
in-input-port-chars
in-lines
in-hash
in-hash-keys
in-hash-values
in-hash-pairs
in-indexed
in-parallel
stop-before
stop-after
make-do-sequence
prop: sequence
3.14.2 Sequence Generators
sequence-generate
Version: 4.0.2

 

3.14 Sequences

Sequence Constructors in Guide: PLT Scheme introduces sequences.

A sequence encapsulates an ordered stream of values. The elements of a sequence can be extracted with one of the for syntactic forms or with the procedures returned by sequence-generate.

The sequence datatype overlaps with many other datatypes. Among built-in datatypes, the sequence datatype includes the following:

In addition, make-do-sequence creates a sequence given a thunk that returns procedures to implement a generator, and the prop:sequence property can be associated with a structure type.

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 cause the bytes to be read from the port.

Inidvidual 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 – a key and its value – for each element in the sequence.

3.14.1 Sequence Predicate and Constructors

(sequence? v)  boolean?

  v : any/c

Return #t if v can be used as a sequence, #f otherwise.

(in-range end)  sequence?

  end : number?

(in-range start end [step])  sequence?

  start : number?

  end : number?

  step : number? = 1

Returns a sequence whose elements are numbers. The single-argument case (in-range end) is equivalent to (in-range 0 end 1). The first number in the sequence is start, and each successive element is generated by adding step to the previous element. The sequence stops before an element that would be greater or equal to end if step is non-negative, or less or equal to end if step is negative.

A in-range application can provide better performance for number iteration when it appears directly in a for clause.

(in-naturals [start])  sequence?

  start : exact-nonnegative-integer? = 0

Returns an infinite sequence of exact integers starting with start, where each element is one more than the preceeding element.

A in-naturals application can provide better performance for integer iteration when it appears directly in a for clause.

(in-list lst)  sequence?

  lst : list?

Returns a sequence equivalent to lst.

A in-list application can provide better performance for list iteration when it appears directly in a for clause.

(in-vector vec)  sequence?

  vec : vector?

Returns a sequence equivalent to vec.

A in-vector application can provide better performance for vector iteration when it appears directly in a for clause.

(in-string str)  sequence?

  str : string?

Returns a sequence equivalent to str.

A in-string application can provide better performance for string iteration when it appears directly in a for clause.

(in-bytes bstr)  sequence?

  bstr : bytes?

Returns a sequence equivalent to bstr.

A in-bytes application can provide better performance for byte string iteration when it appears directly in a for clause.

(in-input-port-bytes in)  sequence?

  in : input-port?

Returns a sequence equivalent to in.

(in-input-port-chars in)  sequence?

  in : input-port?

Returns a sequence whose elements are read as characters form in (as opposed to using in directly as a sequence to get bytes).

(in-lines [in mode])  sequence?

  in : input-port? = (current-input-port)

  

mode

 

:

 

(one-of 'linefeed 'return 'return-linefeed 'any 'any-one)

 

 

 

=

 

'any

Returns a sequence whose elements are the result of (read-line in mode) until an end-of-line is encountered. Note that the default mode is 'any, whereas the default mode of read-line is 'linefeed.

(in-hash hash)  sequence?

  hash : hash?

Returns a sequence equivalent to hash.

(in-hash-keys hash)  sequence?

  hash : hash?

Returns a sequence whose elements are the keys of hash.

(in-hash-values hash)  sequence?

  hash : hash?

Returns a sequence whose elements are the values of hash.

(in-hash-pairs hash)  sequence?

  hash : hash?

Returns a sequence whose elements are pairs, each containing a key and its value from hash (as opposed to using hash directly as a sequence to get the key and value as separate values for each element).

(in-indexed seq)  sequence?

  seq : sequence?

Returns a sequence where each element has two values: the value produced by seq, and a non-negative exact integer starting with 0. The elements of seq must be single-valued.

(in-parallel seq ...)  sequence?

  seq : sequence?

Returns a sequence where each element has as many values as the number of supplied seqs; the values, in order, are the values of each seq. The elements of each seq must be single-valued.

(stop-before seq pred)  sequence?

  seq : sequence?

  pred : (any/c . -> . any)

Returns a sequence that contains the elements of seq (which must be single-valued), but only until the last element for which applying pred to the element produces #t, after which the sequence ends.

(stop-after seq pred)  sequence?

  seq : sequence?

  pred : (any/c . -> . any)

Returns a sequence that contains the elements of seq (which must be single-valued), but only until the element (inclusive) for which applying pred to the element produces #t, after which the sequence ends.

(make-do-sequence thunk)  sequence?

  

thunk

 

:

 

(-> (values (any/c . -> . any)

            (any/c . -> . any/c)

            any/c

            (any/c . -> . any/c)

            (() () #:rest list? . ->* . any/c)

            ((any/c) () #:rest list? . ->* . any/c)))

Returns a sequence whose elements are generated by the procedures and initial value returned by the thunk. The generator is defined in terms of a position, which is initialized to the third result of the thunk, and the element, which may consist of multiple values.

The thunk results define the generated elements as follows:

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 the other two functions always return #t.

prop:sequence : struct-type-property?

Associates a procedure to a structure type that takes an instance of the structure and returns a sequence. If v is an instance of a structure type with this property, then (sequence? v) produces #t.

Examples:

  > (define-struct train (car next)

      #:property prop:sequence (lambda (t)

                                 (make-do-sequence

                                  (lambda ()

                                    (values train-car

                                            train-next

                                            t

                                            (lambda (t) t)

                                            (lambda (v) #t)

                                            (lambda (t v) #t))))))

  > (for/list ([c (make-train 'engine

                              (make-train 'boxcar

                                          (make-train 'caboose

                                                      #f)))])

      c)

  (engine boxcar caboose)

3.14.2 Sequence Generators

(sequence-generate seq)

 

 

(-> boolean?)

 

(-> any)

  seq : sequence?

Returns two thunks to extract elements from the sequence. The first returns #t if more values are available for the sequence. The second returns the next element (which may be multiple values) from the sequence; if no more elements are available, the exn:fail:contract exception is raised.