[plt-scheme] Iterating through hash table?

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Sat Jul 21 12:13:44 EDT 2007

1. Use a let/ec.

(define (exit-from-for-each.v1 ht)
   (let/ec done
     (hash-table-for-each ht (lambda (k x) (if (= x 0) (done 'out!)  
(display x))))))

2. Use call/cc (or let/cc) just in case you need to resume:

(define (exit-from-for-each.v2 ht)
   (let/cc done
     (hash-table-for-each ht (lambda (k x) (if (= x 0) (done 'out!)  
(display x))))))

3. Use an exception.

(define (exit-from-for-each.v3 ht)
   (with-handlers ((symbol? (lambda (x) x)))
     (hash-table-for-each ht (lambda (k x) (if (= x 0) (raise 'out!)  
(display x))))))

4. Or look in control.ss for prompts and abort.ss.

-- Matthias



On Jul 21, 2007, at 12:05 PM, Majorinc, Kazimir wrote:

> How can I iterate through hash-table and stop iterating when some  
> condition is satisfied, not waiting until hash-table-for-each run  
> through all keys in hash table?
>
> Thanks,
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.