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

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon May 7 16:34:55 EDT 2007

Guys, use the force! Eh, prompts. -- Matthias



On May 7, 2007, at 4:26 PM, jos koot wrote:

> Hi Jens Axel
> You may want to take a look in the cookbook recipe "coroutines".
> Jos
>
> (((((lambda(x)((((((((x x)x)x)x)x)x)x)x))
>     (lambda(x)(lambda(y)(x(x y)))))
>    (lambda(x)(x)x))
>   (lambda()(printf "Greetings, Jos~n"))))
> ----- Original Message -----
> From: Jens Axel Søgaard
> To: PLT-list
> Sent: Monday, May 07, 2007 9:53 PM
> Subject: [plt-scheme] Hash-tables and call/cc
>
> Hi all,
>
> I'd like to iterate through the keys in a hash-table
> with srfi-42. In order to do that I'd like a function:
>
>    hash-table->generator : hash-table -> thunk
>
> which takes a hash-table as input and returns a
> thunk g as output. The first invocation of g
> returns the first element, the second invocation
> returns the seconc, and so on. When there is no
> left, g simply return #f.
>
> Here is an attempt do just that:
>
> (define (hash-table->generator ht)
>    (let ([continue 'start])
>      (lambda ()
>        (case continue
>          [(done)    #f]
>          [(start)   (let/ec return
>                       (hash-table-for-each
>                        ht
>                        (lambda (x v)
>                          (call/cc
>                           (lambda (k)
>                             (set! continue k)
>                             (return x)))))
>                       (set! continue 'done)
>                       #f)]
>          [else      (continue)]))))
>
> It almost work - but only almost.
>
> Here is a session in the REPL:
>
>  > (define g (hash-table->generator
>               (make-immutable-hash-table
>                '(("1" . 1) ("2" . 2) ("3" . 3)))))
>  > (g)
> "2"
>  > (g)
> "3"
>  > (g)
> "1"
>  > (g)
> #f
>
> But now the fun begins:
>
>  > (define g (hash-table->generator
>               (make-immutable-hash-table
>                '(("1" . 1) ("2" . 2) ("3" . 3)))))
>  > (list (g) (g))
> (#f done)
>
> I have missed /something/ in hash-table->generator,
> but what?
>
> -- 
> Jens Axel Søgaard
>
>
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.