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

From: Christophe Poucet (christophe.poucet at gmail.com)
Date: Mon May 7 17:21:52 EDT 2007

In general, whenever I want to lazify something, I'll use shift and reset,
cause then you don't have to deal with the issue of capturing the calling
code.  That's what shift+reset are made for, to nicely delimit what it is you
want to capture the continuation of :)

Jens Axel Søgaard wrote:
> 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]))))
> 


-- 
christophe poucet
phd. student
nes - mpsoc ltr

phone:+32 16 28 87 20
e-mail: christophe (dot) poucet (at) imec (dot) be
--------------------------------------------------------------------------------
IMEC vzw – Register of Legal Entities Leuven VAT BE 0425.260.668
         – Kapeldreef 75, B-3001 Leuven, Belgium
         – http://www.imec.be
--------------------------------------------------------------------------------


Posted on the users mailing list.