Subject: Re: Getting a random hash-table element?
From: rpw3@rpw3.org (Rob Warnock)
Date: Mon, 03 Dec 2007 04:05:56 -0600
Newsgroups: comp.lang.lisp
Message-ID: <zoidndOCd5OZSM7anZ2dnUVZ_r-vnZ2d@speakeasy.net>
Damien Kick  <dkixk@earthlink.net> wrote:
+---------------
| > (defun random-ex-hash-key (table)
| >   (loop repeat (1+ (random (hash-table-count table)))
| >       for x being the hash-key of table
| >       finally (return x)))
| 
| BTW, shouldn't this be (loop ... for x being the hash-keys ...).
| I thought it was either BEING EACH HASH-KEY or BEING THE HASH-KEYS.
+---------------

Nope, LOOP's syntax isn't really that "grammatical".  ;-}
It's more of a "pick one from each of columns A, B, & C"
sort of thing, with column A being "{each | the}", column B
being "{hash-key | hash-keys}", and column C being "{in | of}".
Here's the relevant bit from CLHS "Macro LOOP":

    for-as-hash::= var [type-spec] being {each | the}  
		   {{hash-key | hash-keys} {in | of} hash-table 
		   [using (hash-value other-var)] | 
		   {hash-value | hash-values} {in | of} hash-table 
		   [using (hash-key other-var)]} 

So both of the forms "each hash-keys" and "the hash-key" are legal
in LOOP, regardless of what your high-school English teacher might
have to say about them.  ;-}


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607