[CM] iterating over a hash table

Michael Winter mwinter at unboundedpress.org
Wed Dec 16 03:57:44 PST 2009


Hmmm... I am not sure... I am new to scheme and common music (have a java
background). I ended up finalizing the entropy calculator otherwise just
using lists. See below:

(define (entropy string)
  (let ((sum 0) (table '()) (len (length string)))
    (loop for i in string
          do (let ((pair (assoc i table)))
               (if (eq? pair #f)
                 (set! table (append table (list (list i 1))))
                 (set-cdr! pair (list (+ 1 (cadr pair)))))))
    (loop for i in table
          do (let ((prob (/ (cadr i) (exact->inexact len))))
               (set! sum (+ sum (* prob (/ (log prob) (log 2.)))))))
    (set! sum (- sum))
    (display sum)))

(entropy '(1 0 1 0 1 0 1 0))

-----------------------

Speed-wise, how does referencing a hash table compare with using (assoc x y)
as I am above. Is it also just hashed values?

I guess I would prefer hash-table-for-each otherwise you have to keep in
mind to avoid concurrent modification (right?). I am absent-minded enough to
do just that.

Also, I am the guy who wanted to run another lisp interpreter (greg
chaitin's interpreter). I came up with a bit of a nasty hack that seems to
work ok. I set up a osc connection between scheme and a modified version of
chaitin's interpreter running in the background. Then I just send it strings
of m-expressions that are interpreted and then returned.

Thanks for your help, whatever you may hash out... (terrible joke I know).

Best,

Mike


On 12/16/09 3:15 AM, "Bill Schottstaedt" <bil at ccrma.Stanford.EDU> wrote:

>> In the s-7 documentation, it says that a hash-table is a vector of
>> alists and can be iterated over by vector-ref.
> 
> oops -- thanks for pointing this out.  Now I can't decide whether to
> re-enable vector-ref of a hash-table or provide hash-table-for-each --
> any strong opinions?
> 




More information about the Cmdist mailing list