<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content="MSHTML 6.00.6000.16414" name=GENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=#ffffff>
<DIV>Hi Jens Axel</DIV>
<DIV>You may want to take a look in the cookbook recipe "coroutines".</DIV>
<DIV>Jos</DIV>
<DIV> </DIV>
<DIV>(((((lambda(x)((((((((x x)x)x)x)x)x)x)x))<BR>
(lambda(x)(lambda(y)(x(x y)))))<BR> (lambda(x)(x)x))<BR>
(lambda()(printf "Greetings, Jos~n"))))</DIV>
<BLOCKQUOTE
style="PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
<DIV style="FONT: 10pt arial">----- Original Message ----- </DIV>
<DIV
style="BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: black"><B>From:</B>
<A title=jensaxel@soegaard.net href="mailto:jensaxel@soegaard.net">Jens Axel
Søgaard</A> </DIV>
<DIV style="FONT: 10pt arial"><B>To:</B> <A title=plt-scheme@list.cs.brown.edu
href="mailto:plt-scheme@list.cs.brown.edu">PLT-list</A> </DIV>
<DIV style="FONT: 10pt arial"><B>Sent:</B> Monday, May 07, 2007 9:53 PM</DIV>
<DIV style="FONT: 10pt arial"><B>Subject:</B> [plt-scheme] Hash-tables and
call/cc</DIV>
<DIV><BR></DIV>Hi all,<BR><BR>I'd like to iterate through the keys in a
hash-table<BR>with srfi-42. In order to do that I'd like a
function:<BR><BR> hash-table->generator : hash-table ->
thunk<BR><BR>which takes a hash-table as input and returns a<BR>thunk g as
output. The first invocation of g<BR>returns the first element, the second
invocation<BR>returns the seconc, and so on. When there is no<BR>left, g
simply return #f.<BR><BR>Here is an attempt do just that:<BR><BR>(define
(hash-table->generator ht)<BR> (let ([continue
'start])<BR> (lambda
()<BR> (case
continue<BR>
[(done)
#f]<BR> [(start)
(let/ec
return<BR>
(hash-table-for-each<BR>
ht<BR>
(lambda (x
v)<BR>
(call/cc<BR>
(lambda
(k)<BR>
(set! continue
k)<BR>
(return
x)))))<BR>
(set! continue
'done)<BR>
#f)]<BR>
[else (continue)]))))<BR><BR>It almost work -
but only almost.<BR><BR>Here is a session in the REPL:<BR><BR> >
(define g
(hash-table->generator<BR>
(make-immutable-hash-table<BR>
'(("1" . 1) ("2" . 2) ("3" . 3)))))<BR> > (g)<BR>"2"<BR> >
(g)<BR>"3"<BR> > (g)<BR>"1"<BR> > (g)<BR>#f<BR><BR>But now the
fun begins:<BR><BR> > (define g
(hash-table->generator<BR>
(make-immutable-hash-table<BR>
'(("1" . 1) ("2" . 2) ("3" . 3)))))<BR> > (list (g) (g))<BR>(#f
done)<BR><BR>I have missed /something/ in hash-table->generator,<BR>but
what?<BR><BR>-- <BR>Jens Axel
Søgaard<BR><BR><BR>_________________________________________________<BR>
For list-related administrative tasks:<BR> <A
href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</A><BR></BLOCKQUOTE></BODY></HTML>