From: mtg
Subject: increment hash value
Date: 
Message-ID: <1177487303.121065.120280@r35g2000prh.googlegroups.com>
Moin,

I try to count list items with the following function:

(defun count-items (lst)
  (let ((ht (make-hash-table)))
    (loop :for item :in lst :do
       (incf (gethash item ht 0))
       :finally
       (maphash #'(lambda (k v) (format t "~A: ~A~%" k v)) ht))))

(count-tags '('foo 'bar 'foo)) gives me

'FOO: 1
'BAR: 1
'FOO: 1

but I need
'FOO: 2
'BAR: 1

What ist wrong with my function? Is there a better way to count items?
Many thanks
Matthias

From: Alex Mizrahi
Subject: Re: increment hash value
Date: 
Message-ID: <462f2057$0$90268$14726298@news.sunsite.dk>
(message (Hello 'mtg)
(you :wrote  :on '(25 Apr 2007 00:48:23 -0700))
(

 m> (count-tags '('foo 'bar 'foo)) gives me

'('foo 'bar 'foo) is not a list of symbols but a list of lists.
by default hash-table has test EQL that does not work for lists comparison. 
if you do (make-hash-table :test 'equal) it should work well.
(but looks like you've just incorrectly wrote your data)

)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"I am everything you want and I am everything you need") 
From: Madhu
Subject: Re: increment hash value
Date: 
Message-ID: <m3hcr4siaj.fsf@robolove.meer.net>
Since Kent Pitman is around, I'd like to get clarification on the
X3J1J cleanup issue `SETF-GET-DEFAULT Writeup', [which you can access
from the hyperspec page for GETHASH]


* mtg  <························@r35g2000prh.googlegroups.com> :
| (defun count-items (lst)
|   (let ((ht (make-hash-table)))
|     (loop :for item :in lst :do
|        (incf (gethash item ht 0))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|        :finally
|        (maphash #'(lambda (k v) (format t "~A: ~A~%" k v)) ht))))


Doesn't that issue imply that this idiom, (incf (GETHASH ITEM HT 0)),
is not portable, and an implementation is free to not actually update
the place referred to by (GETHASH ITEM HT) ?

--
Madhu
From: Alex Mizrahi
Subject: Re: increment hash value
Date: 
Message-ID: <462f65f9$0$90263$14726298@news.sunsite.dk>
(message (Hello 'Madhu)
(you :wrote  :on '(Wed, 25 Apr 2007 18:49:32 +0530))
(

 M> Doesn't that issue imply that this idiom, (incf (GETHASH ITEM HT 0)),
 M> is not portable, and an implementation is free to not actually update
 M> the place referred to by (GETHASH ITEM HT) ?

why do you think so?
the problem in the message is because (EQL '(QUOTE FOO) '(QUOTE FOO)) => 
NIL.
have you really seen a problem with (incf (GETHASH ITEM HT 0))???

)
(With-best-regards '(Alex Mizrahi) :aka 'killer_storm)
"I am everything you want and I am everything you need") 
From: Madhu
Subject: Re: increment hash value
Date: 
Message-ID: <m3slao7x92.fsf@robolove.meer.net>
* "Alex Mizrahi" <·························@news.sunsite.dk> :
|  M> Doesn't that issue imply that this idiom, (incf (GETHASH ITEM HT 0)),
|  M> is not portable, and an implementation is free to not actually update
|  M> the place referred to by (GETHASH ITEM HT) ?
|
| why do you think so?
| have you really seen a problem with (incf (GETHASH ITEM HT 0))???

No I have never seen a problem with this.  I understand that INCF will
update the place referred to by (GETHASH ITEM HT) by calling by
calling its setf-expander, and this is what CL stipulates.

On second thoughts, I withdraw my question, as I don't believe it
makes sense.
--
Madhu