[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: simpler srfi 45 implementation



Just to avoid confusion.

  This version is not safe for space and
  does not implement SRFI-45.

Andre

On Wed, 14 Nov 2007, Phil Bewig wrote:

Here is the code for stream-type, extracted from the latest version of
SRFI-45 (at this writing, that version has been submitted to the editors but
has not yet appeared on srfi.schemers.org).

 (define-record-type (stream-type make-stream stream?)
   (fields (mutable box stream-promise stream-promise!)))

 (define-syntax stream-lazy
   (syntax-rules ()
     ((stream-lazy exp) (make-stream (lambda () exp)))))

 (define-syntax stream-delay
   (syntax-rules ()
     ((stream-delay exp) (stream-lazy (make-stream (list exp))))))

 (define (stream-force prom)
   (let ((p (stream-promise prom)))
     (cond ((pair? p)   (car p))
           ((stream? p)
             (let ((x (stream-force p)))
               (let ((content (stream-promise prom)))
                 (if (pair? content) (cdr content)
                     (begin (stream-promise! prom (list x)) x)))))
           ((procedure? p)
             (let ((prom* (p)))
               (if (not (pair? (stream-promise prom)))
                   (if (stream? prom*)
                       (begin (stream-promise! prom (stream-promise prom*))
                              (stream-promise! prom* prom))
                       (stream-promise! prom (list prom*))))
               (stream-force prom)))
           (else (error 'stream-force "invalid promise")))))