s c h e m a t i c s : c o o k b o o k

/ NoParentSelected / Cookbook.HashRecipeTraverse

This Web


WebHome 
WebChanges 
TOC (with recipes)
NewRecipe 
WebTopicList 
WebStatistics 

Other Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
Sourceforge Page
SchemeWiki.org
Original Cookbook
RSS

Scheme Links


Schemers.org
Scheme FAQ
R5RS
SRFIs
Scheme Cross Reference
PLT Scheme SISC
Scheme48 SCM
MIT Scheme scsh
JScheme Kawa
Chicken Guile
Bigloo Tiny
Gambit LispMe
GaucheChez

Lambda the Ultimate
TWiki.org

Traversing a Hash

Problem

You want to perform an action on each entry in a hash.

Solution

As with lists, see chapter ListRecipes, there are two basic ways to go over the elements of a hash table applying a procedure on each one. PLT provides hash-table-map, which accumulates the results into a list, and hash-table-for-each, which applies the procedure to the elements in the hash, but returns void.

(let ((t (make-immutable-hash-table '((1 . "one") (2 . "two")))))
  (values (hash-table-map t (lambda (key value)
                              (string-append "Foo:" value)))
          (hash-table-for-each t (lambda (key value)
                                   (display value)))))

Note that the procedure passed as the second argument receives two arguments, the first one is the key, and the second the value.


Comments about this recipe

It would be so much better to have the order of the arguments to hash-table-map, and hash-table-get reversed, to match the behavior of map, for-each, etc.

Contributors

-- KarlaRamirez - 19 May 2004

CookbookForm
TopicType: Recipe
ParentTopic: HashRecipes
Next Topic:

 
 
Copyright © 2004 by the contributing authors. All material on the Schematics Cookbook web site is the property of the contributing authors.
The copyright for certain compilations of material taken from this website is held by the SchematicsEditorsGroup - see ContributorAgreement & LGPL.
Other than such compilations, this material can be redistributed and/or modified under the terms of the GNU Lesser General Public License (LGPL), version 2.1, as published by the Free Software Foundation.
Ideas, requests, problems regarding Schematics Cookbook? Send feedback.
/ You are Main.guest