Next: , Previous: , Up: streams   [Index]


1.15.7 Range streams

Function: stream-from first
Function: stream-from first step

Create a newly–allocated stream that contains first as its first element and increments each succeeding element by step, which defaults to ‘1’. first and step may be of any numeric type.

stream-from is frequently useful as a generator in stream-of expressions. See also stream-range for a similar procedure that creates finite streams.

stream-from could be implemented as:

(stream-iterate
    (lambda (x)
      (+ x step))
  first)

Example:

(define nats (stream-from 0))
⇒ 0 1 2 ...

(define odds (stream-from 1 2))
⇒ 1 3 5 ...
Function: stream-range first past
Function: stream-range first past step

Create a newly–allocated stream that contains first as its first element and increments each succeeding element by step. The stream is finite and ends before past, which is not an element of the stream. If step is not given it defaults to ‘1’ if first is less than past and ‘-1’ otherwise.

first, past and step may be of any numeric type.

stream-range is frequently useful as a generator in stream-of expressions. See also stream-from for a similar procedure that creates infinite streams.

Examples:

(stream-range 0 10)
⇒ 0 1 2 3 4 5 6 7 8 9

(stream-range 0 10 2)
⇒ 0 2 4 6 8

Successive elements of the stream are calculated by adding step to first, so if any of first, past or step are inexact, the length of the output stream may differ from:

(ceiling (- (/ (- past first) step) 1)

Next: , Previous: , Up: streams   [Index]