Next: , Previous: , Up: SRFI-41   [Contents][Index]


7.5.28.2 SRFI-41 Stream Primitives

This library provides eight operators: constructors for stream-null and stream-pairs, type predicates for streams and the two kinds of streams, accessors for both fields of a stream-pair, and a lambda that creates procedures that return streams.

Scheme Variable: stream-null

A promise that, when forced, is a single object, distinguishable from all other objects, that represents the null stream. stream-null is immutable and unique.

Scheme Syntax: stream-cons object-expr stream-expr

Creates a newly-allocated stream containing a promise that, when forced, is a stream-pair with object-expr in its stream-car and stream-expr in its stream-cdr. Neither object-expr nor stream-expr is evaluated when stream-cons is called.

Once created, a stream-pair is immutable; there is no stream-set-car! or stream-set-cdr! that modifies an existing stream-pair. There is no dotted-pair or improper stream as with lists.

Scheme Procedure: stream? object

Returns true if object is a stream, otherwise returns false. If object is a stream, its promise will not be forced. If (stream? obj) returns true, then one of (stream-null? obj) or (stream-pair? obj) will return true and the other will return false.

Scheme Procedure: stream-null? object

Returns true if object is the distinguished null stream, otherwise returns false. If object is a stream, its promise will be forced.

Scheme Procedure: stream-pair? object

Returns true if object is a stream-pair constructed by stream-cons, otherwise returns false. If object is a stream, its promise will be forced.

Scheme Procedure: stream-car stream

Returns the object stored in the stream-car of stream. An error is signalled if the argument is not a stream-pair. This causes the object-expr passed to stream-cons to be evaluated if it had not yet been; the value is cached in case it is needed again.

Scheme Procedure: stream-cdr stream

Returns the stream stored in the stream-cdr of stream. An error is signalled if the argument is not a stream-pair.

Scheme Syntax: stream-lambda formals body …

Creates a procedure that returns a promise to evaluate the body of the procedure. The last body expression to be evaluated must yield a stream. As with normal lambda, formals may be a single variable name, in which case all the formal arguments are collected into a single list, or a list of variable names, which may be null if there are no arguments, proper if there are an exact number of arguments, or dotted if a fixed number of arguments is to be followed by zero or more arguments collected into a list. Body must contain at least one expression, and may contain internal definitions preceding any expressions to be evaluated.

(define strm123
  (stream-cons 1
    (stream-cons 2
      (stream-cons 3
        stream-null))))

(stream-car strm123) ⇒ 1
(stream-car (stream-cdr strm123) ⇒ 2

(stream-pair?
  (stream-cdr
    (stream-cons (/ 1 0) stream-null))) ⇒ #f

(stream? (list 1 2 3)) ⇒ #f

(define iter
  (stream-lambda (f x)
    (stream-cons x (iter f (f x)))))

(define nats (iter (lambda (x) (+ x 1)) 0))

(stream-car (stream-cdr nats)) ⇒ 1

(define stream-add
  (stream-lambda (s1 s2)
    (stream-cons
      (+ (stream-car s1) (stream-car s2))
      (stream-add (stream-cdr s1)
                  (stream-cdr s2)))))

(define evens (stream-add nats nats))

(stream-car evens) ⇒ 0
(stream-car (stream-cdr evens)) ⇒ 2
(stream-car (stream-cdr (stream-cdr evens))) ⇒ 4

Next: , Previous: , Up: SRFI-41   [Contents][Index]