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.
(stream-cons first-expr rest-expr)
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.
(stream expr ...)
(stream* expr ... rest-expr)
Added in version 6.3 of package base.
Changed in version 184.108.40.206 of package base: Improved element-reachability guarantee for streams in for.
In the case of lazy streams, this function forces evaluation only of the sub-streams, and not the stream’s elements.
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.
(for/stream (for-clause ...) body-or-break ... body)
(for*/stream (for-clause ...) body-or-break ... body)
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.
> (for/stream ([i '(1 2 3)]) (* i i))
> (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)
> (stream-ref (for/stream ([i (in-naturals)]) (* i i)) 25)
Added in version 220.127.116.11 of package base.
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
> (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)
> (stream-first l1)
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 18.104.22.168 of package base.