► |  | The Racket Reference

1 | Language Model
2 | Notation for Documentation
3 | Syntactic Forms
4 | Datatypes
5 | Structures
6 | Classes and Objects
7 | Units
8 | Contracts
9 | Pattern Matching
10 | Control Flow
11 | Concurrency and Parallelism
12 | Macros
13 | Input and Output
14 | Reflection and Security
15 | Operating System
16 | Memory Management
17 | Unsafe Operations
18 | Running Racket
| Bibliography
| Index

► | 4 | Datatypes

4.1 | Booleans and Equality
4.2 | Numbers
4.3 | Strings
4.4 | Byte Strings
4.5 | Characters
4.6 | Symbols
4.7 | Regular Expressions
4.8 | Keywords
4.9 | Pairs and Lists
4.10 | Mutable Pairs and Lists
4.11 | Vectors
4.12 | Boxes
4.13 | Hash Tables
4.14 | Sequences and Streams
4.15 | Dictionaries
4.16 | Sets
4.17 | Procedures
4.18 | Void
4.19 | Undefined

▼ | 4.14 | Sequences and Streams

4.14.1 | Sequences
4.14.2 | Streams
4.14.3 | Generators

On this page:

stream?
stream-empty?
stream-first
stream-rest
stream-cons
stream
stream*
in-stream
empty-stream
stream->list
stream-length
stream-ref
stream-tail
stream-append
stream-map
stream-andmap
stream-ormap
stream-for-each
stream-fold
stream-count
stream-filter
stream-add-between
for/ stream
for*/ stream
gen: stream
prop: stream
stream/ c

top ← prev up next →

##### 4.14.2`` Streams

A stream is a kind of sequence that supports functional iteration via stream-first and stream-rest. The stream-cons form constructs a lazy stream, but plain lists can be used as streams, and functions such as in-range and in-naturals also create streams.

(require racket/stream) | package: base

The bindings documented in this section are provided by the racket/stream and racket libraries, but not racket/base.

> procedure (stream? v) → boolean?
> v : any/c

Returns #t if v can be used as a stream, #f otherwise.

> procedure (stream-empty? s) → boolean?
> s : stream?

Returns #t if s has no elements, #f otherwise.

> procedure (stream-first s) → any
> s : (and/c stream? (not/c stream-empty?))

Returns the value(s) of the first element in s.

> procedure (stream-rest s) → stream?
> s : (and/c stream? (not/c stream-empty?))

Returns a stream that is equivalent to s without its first element.

> syntax (stream-cons first-expr rest-expr)

Produces a lazy stream for which stream-first forces the evaluation of first-expr to produce the first element of the stream, and stream-rest forces the evaluation of rest-expr to produce a stream for the rest of the returned stream.

The first element of the stream as produced by first-expr must be a single value. The rest-expr must produce a stream when it is evaluated, otherwise the exn:fail:contract? exception is raised.

> syntax (stream expr ...)

A shorthand for nested stream-conses ending with empty-stream.

> syntax (stream* expr ...)

A shorthand for nested stream-conses, but the final expr must be a stream, and it is used as the rest of the stream instead of empty-stream. Similar to list* but for streams.

Added in version 6.3 of package base.

> procedure (in-stream s) → sequence?
> s : stream?

Returns a sequence that is equivalent to s.
An in-stream application can provide better performance for streams iteration when it appears directly in a for clause.
See for for information on the reachability of stream elements during an iteration.

Changed in version 6.7.0.4 of package base: Improved element-reachability guarantee for streams in for.

> value empty-stream : stream?

A stream with no elements.

> procedure (stream->list s) → list?
> s : stream?

Returns a list whose elements are the elements of s, each of which must be a single value. If s is infinite, this function does not terminate.

> procedure (stream-length s) → exact-nonnegative-integer?
> s : stream?

Returns the number of elements of s. If s is infinite, this function does not terminate.

In the case of lazy streams, this function forces evaluation only of the sub-streams, and not the stream’s elements.

> procedure (stream-ref s i) → any
> s : stream?
> i : exact-nonnegative-integer?

Returns the ith element of s (which may be multiple values).

> procedure (stream-tail s i) → stream?
> s : stream?
> i : exact-nonnegative-integer?

Returns a stream equivalent to s, except that the first i elements are omitted.

In case extracting elements from s involves a side effect, they will not be extracted until the first element is extracted from the resulting stream.

> procedure (stream-append s ...) → stream?
> s : stream?

Returns a stream that contains all elements of each stream in the order they appear in the original streams. The new stream is constructed lazily, while the last given stream is used in the tail of the result.

> procedure (stream-map f s) → stream?
> f : procedure?
> s : stream?

Returns a stream that contains f applied to each element of s. The new stream is constructed lazily.

> procedure (stream-andmap f s) → boolean?
> f : (-> any/c ... boolean?)
> s : stream?

Returns #t if f returns a true result on every element of s. If s is infinite and f never returns a false result, this function does not terminate.

> procedure (stream-ormap f s) → boolean?
> f : (-> any/c ... boolean?)
> s : stream?

Returns #t if f returns a true result on some element of s. If s is infinite and f never returns a true result, this function does not terminate.

> procedure (stream-for-each f s) → void?
> f : (-> any/c ... any)
> s : stream?

Applies f to each element of s. If s is infinite, this function does not terminate.

> procedure (stream-fold f i s) → any/c
> f : (-> any/c any/c ... any/c)
> i : any/c
> s : stream?

Folds f over each element of s with i as the initial accumulator. If s is infinite, this function does not terminate.

> procedure (stream-count f s) → exact-nonnegative-integer?
> f : procedure?
> s : stream?

Returns the number of elements in s for which f returns a true result. If s is infinite, this function does not terminate.

> procedure (stream-filter f s) → stream?
> f : (-> any/c ... boolean?)
> s : stream?

Returns a stream whose elements are the elements of s for which f returns a true result. Although the new stream is constructed lazily, if s has an infinite number of elements where f returns a false result in between two elements where f returns a true result, then operations on this stream will not terminate during the infinite sub-stream.

> procedure (stream-add-between s e) → stream?
> s : stream?
> e : any/c

Returns a stream whose elements are the elements of s, but with e between each pair of elements in s. The new stream is constructed lazily.

> syntax (for/stream (for-clause ...) body-or-break ... body)
> syntax (for*/stream (for-clause ...) body-or-break ... body)

Iterates like for/list and for*/list, respectively, but the results are lazily collected into a stream instead of a list.

Unlike most for forms, these forms are evaluated lazily, so each body will not be evaluated until the resulting stream is forced. This allows for/stream and for*/stream to iterate over infinite sequences, unlike their finite counterparts.

Examples:

> > (for/stream ([i '(1 2 3)]) (* i i))
> #<stream>
> > (stream->list (for/stream ([i '(1 2 3)]) (* i i)))
> '(1 4 9)
> > (stream-ref (for/stream ([i '(1 2 3)]) (displayln i) (* i i)) 1)
> 1
> 2
> 4
> > (stream-ref (for/stream ([i (in-naturals)]) (* i i)) 25)
> 625

Added in version 6.3.0.9 of package base.

> value gen:stream : any/c

Associates three methods to a structure type to implement the generic interface (see Generic Interfaces) for streams.

To supply method implementations, the #:methods keyword should be used in a structure type definition. The following three methods should be implemented:

- stream-empty? : accepts one argument

- stream-first : accepts one argument

- stream-rest : accepts one argument

Examples:

> > (define-struct list-stream (v)
> #:methods gen:stream
> [(define (stream-empty? stream)
> (empty? (list-stream-v stream)))
> (define (stream-first stream)
> (first (list-stream-v stream)))
> (define (stream-rest stream)
> (rest (list-stream-v stream)))])
> > (define l1 (list-stream '(1 2)))
> > (stream? l1)
> #t
> > (stream-first l1)
> 1

> value prop:stream : struct-type-property?

A deprecated structure type property used to define custom extensions to the stream API. Use gen:stream instead. Accepts a vector of three procedures taking the same arguments as the methods in gen:stream.

> procedure (stream/c c) → contract?
> c : contract?

Returns a contract that recognizes streams. All elements of the stream must match c.

If the c argument is a flat contract or a chaperone contract, then the result will be a chaperone contract. Otherwise, the result will be an impersonator contract.

When an stream/c contract is applied to an asynchronous channel, the result is not eq? to the input. The result will be either a chaperone or impersonator of the input depending on the type of contract.

Contracts on streams are evaluated lazily by necessity (since streams may be infinite). Contract violations will not be raised until the value in violation is retrieved from the stream. As an exception to this rule, streams that are lists are checked immediately, as if c had been used with listof.

If a contract is applied to a stream, and that stream is subsequently used as the tail of another stream (as the second parameter to stream-cons), the new elements will not be checked with the contract, but the tail’s elements will still be enforced.

Added in version 6.1.1.8 of package base.

top ← prev up next →