Streams (MIT/GNU Scheme 10.1.6)

Next: Weak References, Previous: Promises, Up: Miscellaneous Datatypes [Contents][Index]

### 10.6 Streams

In addition to promises, MIT/GNU Scheme supports a higher-level abstraction called streams. Streams are similar to lists, except that the tail of a stream is not computed until it is referred to. This allows streams to be used to represent infinitely long lists.

procedure: stream object …

  Returns a newly allocated stream whose elements are the arguments. Note that the expression `(stream)` returns the empty stream, or end-of-stream marker.

procedure: list->stream list

  Returns a newly allocated stream whose elements are the elements of list. Equivalent to `(apply stream list)`.

procedure: stream->list stream

  Returns a newly allocated list whose elements are the elements of stream. If stream has infinite length this procedure will not terminate. This could have been defined by

      (define (stream->list stream)
        (if (stream-null? stream)
            '()
            (cons (stream-car stream)
                  (stream->list (stream-cdr stream)))))

special form: cons-stream object expression

  Returns a newly allocated stream pair. Equivalent to `(cons object (delay expression))`.

procedure: stream-pair? object

  Returns `#t` if object is a pair whose cdr contains a promise. Otherwise returns `#f`. This could have been defined by

      (define (stream-pair? object)
        (and (pair? object)
             (promise? (cdr object))))

procedure: stream-car stream
procedure: stream-first stream

  Returns the first element in stream. `stream-car` is equivalent to `car`. `stream-first` is a synonym for `stream-car`.

procedure: stream-cdr stream
procedure: stream-rest stream

  Returns the first tail of stream. Equivalent to `(force (cdr stream))`. `stream-rest` is a synonym for `stream-cdr`.

procedure: stream-null? stream

  Returns `#t` if stream is the end-of-stream marker; otherwise returns `#f`. This is equivalent to `null?`, but should be used whenever testing for the end of a stream.

procedure: stream-length stream

  Returns the number of elements in stream. If stream has an infinite number of elements this procedure will not terminate. Note that this procedure forces all of the promises that comprise stream.

procedure: stream-ref stream k

  Returns the element of stream that is indexed by k; that is, the kth element. K must be an exact non-negative integer strictly less than the length of stream.

procedure: stream-head stream k

  Returns the first k elements of stream as a list. K must be an exact non-negative integer strictly less than the length of stream.

procedure: stream-tail stream k

  Returns the tail of stream that is indexed by k; that is, the kth tail. This is equivalent to performing `stream-cdr` k times. K must be an exact non-negative integer strictly less than the length of stream.

procedure: stream-map procedure stream stream …

  Returns a newly allocated stream, each element being the result of invoking procedure with the corresponding elements of the streams as its arguments.

Next: Weak References, Previous: Promises, Up: Miscellaneous Datatypes [Contents][Index]