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

From: jos koot (jos.koot at telefonica.net)
Date: Mon May 7 16:26:31 EDT 2007

Hi Jens Axel
You may want to take a look in the cookbook recipe "coroutines".

(((((lambda(x)((((((((x x)x)x)x)x)x)x)x))
    (lambda(x)(lambda(y)(x(x y)))))
  (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
                         (lambda (x v)
                            (lambda (k)
                              (set! continue k)
                              (return x)))))
                        (set! continue 'done)
           [else      (continue)]))))

  It almost work - but only almost.

  Here is a session in the REPL:

   > (define g (hash-table->generator
                 '(("1" . 1) ("2" . 2) ("3" . 3)))))
   > (g)
   > (g)
   > (g)
   > (g)

  But now the fun begins:

   > (define g (hash-table->generator
                 '(("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:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070507/20a409a1/attachment.html>

Posted on the users mailing list.