Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Loop over keys and values of a hashtable

7 views
Skip to first unread message

Robert L.

unread,
Sep 19, 2017, 2:10:54 PM9/19/17
to
Edi Weitz wrote:

> > I'm trying to make a loop that runs over all pairs of keys/values of
> > a hashtable. My question is - will the code below always give the
> > key/value pairs that belong to each other. In some implementations
> > it does, but I'm asking whether it is reasonable to expect the
> > keys/values to be paird. My gut feeling is that it's not.
> >
> > For example the following code converting a hashtable to an assoc-list:
> >
> > (loop for key being the hash-keys of *hash*
> > for val being the hash-values of *hash* collect (cons key val))
>
> I think there's nothing in the standard that says the above code
> should do what you expect. Instead, use somthing like this:
>
> (loop for key being the hash-keys of *hash*
> using (hash-value val)
> collect (cons key val))

(define h (hash 'b 2 'c '3 'd 4))

(hash-map h cons)

===>
((c . 3) (b . 2) (d . 4))


--
When the Israeli bombers and torpedo-planes were sent to attack and destroy the
ship, the Jewish commander, seeing that it was an American vessel, had
misgivings and reported to the High Command, which simply repeated the orders
to attack and sink the Liberty. www.revilo-oliver.com/rpo/Bit_of_Good_News.html
0 new messages