Streams

A Stream who's first element is 42 and the rest is unknown, possibly infinite

A Stream is a sequence, much like a list, except that elements beyond the first element are unknown. By unknown we mean that they are delayed and will be evaluated in a lazy manner. Until we consume an unseen element, it's value isn't known or computed. This enables a possibly infinite data structure without hanging the Lisp interpreter.

This implementaiton depends on function_utilities.scm.

Stream Constructor

(define-macro (cons-stream x y)
  (list 'cons x 
        (list 'memo-proc  
              (list 'lambda '() y))))
  

cons-stream constructor for streams. Given two elements x and y, it creates a cons pair where the first item is x and the second item is the rest of the stream.

We implement cons-stream as a special form to enable lazy evaluation.

Stream Selectors

(define (stream-car s) 
  (car s))
  

stream-car gives us the first element of the stream. Analygous to car.

(define (stream-cdr s) 
  ((cdr s)))
  

stream-cdr gives us the rest of the elements in the stream.
Note: extra layer of parens forces the delayed stream.

Stream Useful Pieces

(define the-empty-stream '())

Suprise, the-empty-stream is the empty or null stream.

(define (stream-null? s) 
  (eq? s the-empty-stream))

stream-null? is true if the stream s is the-empty-stream