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

From: Eli Barzilay (eli at barzilay.org)
Date: Mon May 7 16:18:56 EDT 2007

On May  7, Jens Axel Søgaard wrote:
> 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:
> [...]

I don't know what's wrong in your version (the escape continuation
seemed fishy, but it didn't matter if I changed it to a cc).  But I
tried the Swindle version, and it worked fine:

  => (define t (make-immutable-hash-table '(("1" . 1) ("2" . 2) ("3" . 3))))
  => (list-of x [x <- t])
  (("1" . 1) ("2" . 2) ("3" . 3))

To make it work more like your version (using only the keys):

  => (define ((hash-table-iterator ht) yield)
       (hash-table-for-each ht (lambda (k v) (yield k))))
  => (list-of x [x <- (hash-table-iterator t)])
  ("1" "2" "3")

and using the generator directly works too:

  => (define g (function-iterator (hash-table-iterator t)))
  => (g)
  "1"
  => (g)
  "2"
  => (g)
  "3"
  => (g)
  => (g)
  function-iterator: iterated function #<procedure:hash-table-iterator:1> exhausted.

and:

  => (define g (function-iterator (hash-table-iterator t)))
  => (list (g) (g))
  ("1" "2")
  => (list (g) (g))
  ("3" #<void>)

So perhaps the `function-iterator' would be a good starting point?

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.