Re: SRFI-40

David Van Horn <[email protected]> Wed, 22 Sep 2004 17:31:40 -0400
Newsgroups gmane.lisp.scheme.plt.schematics
Message-ID <[email protected]>
Noel Welsh wrote:
> --- Gordon Weakliem <[email protected]> wrote:
>>I've put a version of SRFI - 40 into the SRFI area. 
> 
> Cool.  I guess Francisco will add this to the PLT SRFI
> collection.  If you are feeling energetic you might want to
> flesh out IdiomStreams in the Cookbook.

As noted on the SRFI-40 post-finalization list, the reference implementation 
has problems, which were solved, but not included in the final version of the 
document.  So the implementation, as given, shouldn't be used.  I'm going to 
update the reference implementation shortly (in the next day or so).  I'll 
send a PLT version to this list afterward.

In the meantime, I have a PLT implementation of SRFI 45, which is attached. 
There are two files because I have to jump through the usual hoops that come 
with name conflicts.  lazy.ss provides srfi-45: prefixed names and 45.ss 
provides unprefixed names.

David
lazy.ss (text/plain, 1.1 KB)
(module lazy mzscheme
  (provide srfi-45:lazy srfi-45:eager srfi-45:delay srfi-45:force)
  
  (define-struct s:promise (kind content))
  
  (define-syntax srfi-45:lazy
     (syntax-rules ()
       ((lazy exp)
        (box (make-s:promise 'lazy (lambda () exp))))))
  
  (define (srfi-45:eager x)
    (box (make-s:promise 'eager x)))
  
  (define-syntax srfi-45:delay
    (syntax-rules ()
      ((srfi-45:delay exp) (srfi-45:lazy (srfi-45:eager exp)))))
  
  (define (srfi-45:force promise)
    (let ((content (unbox promise)))
      (case (s:promise-kind content)
        ((eager) (s:promise-content content))
        ((lazy) 
         (let* ((promise* ((s:promise-content content)))
                (srfi-45:content  (unbox promise)))
           (if (not (eqv? 'eager (s:promise-kind content)))
               (begin 
                 (set-s:promise-kind! content (s:promise-kind (unbox promise*)))
                 (set-s:promise-content! content (s:promise-content (unbox promise*)))
                 (set-box! promise* content)))
           (srfi-45:force promise))))))  
)
45.ss (text/plain, 219 B)
(module |45| mzscheme
  (require (lib "lazy.ss" "srfi"))
  (provide (rename srfi-45:delay delay )
           (rename srfi-45:eager eager)
           (rename srfi-45:force force)
           (rename srfi-45:lazy  lazy)))