[plt-scheme] Hash-tables and call/cc

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Mon May 7 16:55:30 EDT 2007

Christophe Poucet wrote:

> (module foo
>   (require (lib "control.ss"))
> 
>   (define (hash-table->generator ht)
>     (let ([*pair*
>             (reset
>               (hash-table-for-each
>                 ht
>                 (lambda (x v)
> 		;; Return the value and the remaining computation
>                   (shift f (cons f v))))
> 	      ;; Return a final #f
>               (shift f #f))])
>       (lambda ()
>         (if *pair*
>           (let ([temp *pair*])
> 	    ;; The #f is just a bogus parameter, reset continuations
> 	    ;; require 1 parameter
>             (set! *pair* ((car temp) #f))
>             (cdr temp)
>             )
>           #f))))

Thanks, I'd better head over to Kyle's tutorial and see
what else (lib "control.ss") has in store.

Here is the same version with slightly different naming:

(require (lib "control.ss")
          (lib "match.ss"))

(define (hash-table->generator ht)
   (let ([continue+x
          (reset
           (hash-table-for-each
            ht
            (lambda (x v)
              ;; Return the value and the remaining computation
              (shift f (cons f v))))
           ;; Return a final #f
           (shift f #f))])
     (lambda ()
       (match continue+x
         [(continue . x)  (set! continue+x (continue 'dummy))
                          x]
         [#f              #f]))))

-- 
Jens Axel Søgaard



Posted on the users mailing list.