From: Vladimir Zolotykh
Subject: Puzzled with hash table
Date: 
Message-ID: <3E8AE61C.6080609@eurocom.od.ua>
Hi,

Here is the simple function that reveals some feature of hash tables I
don't understand or more likely my misunderstanding of them. Could you
help me please ?

   (defun foo (n)
     (let ((h (make-hash-table :test #'equal)))
       (setf (gethash 1 h) `("foo" 1)
	    (gethash 2 h) `("bar" 1))
       (let ((pair (gethash n h)))
	(decf (second pair)))
       (loop for v being the hash-value in h do (print v))))

Being called (compiled) this function prints

   cl-user(119): (foo 2)

   ("foo" 1)
   ("bar" 0)
   nil
   cl-user(120): (foo 2)

   ("foo" 1)
   ("bar" -1)
   nil
   cl-user(121): (foo 2)

   ("foo" 1)
   ("bar" -2)
   nil

both in ACL 6.2 and CMUCL 18c. My expectations was to see

   ("foo" 1)
   ("bar" 0)

each the time.

What's wrong with my code ?

Thanks in advance

-- 
Vladimir Zolotykh

From: Matthieu Villeneuve
Subject: Re: Puzzled with hash table
Date: 
Message-ID: <3e8aeb46$0$26877$626a54ce@news.free.fr>
"Vladimir Zolotykh" <······@eurocom.od.ua> wrote in message
·····················@eurocom.od.ua...
> Hi,
>
> Here is the simple function that reveals some feature of hash tables I
> don't understand or more likely my misunderstanding of them. Could you
> help me please ?

The same old problem, DECF is applied to the SECOND of the _literal_ value
`("bar" 1)...

>    (defun foo (n)
>      (let ((h (make-hash-table :test #'equal)))
>        (setf (gethash 1 h) `("foo" 1)
>     (gethash 2 h) `("bar" 1))
>        (let ((pair (gethash n h)))
> (decf (second pair)))
>        (loop for v being the hash-value in h do (print v))))
>
> Being called (compiled) this function prints
>
>    cl-user(119): (foo 2)
>
>    ("foo" 1)
>    ("bar" 0)
>    nil
>    cl-user(120): (foo 2)
>
>    ("foo" 1)
>    ("bar" -1)
>    nil
>    cl-user(121): (foo 2)
>
>    ("foo" 1)
>    ("bar" -2)
>    nil
>
> both in ACL 6.2 and CMUCL 18c. My expectations was to see
>
>    ("foo" 1)
>    ("bar" 0)
>
> each the time.
>
> What's wrong with my code ?
>
> Thanks in advance
>
> --
> Vladimir Zolotykh
>

--
Matthieu Villeneuve
From: Matthieu Villeneuve
Subject: Re: Puzzled with hash table
Date: 
Message-ID: <3e8aebc6$0$26848$626a54ce@news.free.fr>
"Matthieu Villeneuve" <········@nospam.matthieu-villeneuve.net> wrote in
message ······························@news.free.fr...
> The same old problem, DECF is applied to the SECOND of the _literal_ value
> `("bar" 1)...
>
> >    (defun foo (n)
> >      (let ((h (make-hash-table :test #'equal)))
> >        (setf (gethash 1 h) `("foo" 1)
> >     (gethash 2 h) `("bar" 1))
> >        (let ((pair (gethash n h)))
> > (decf (second pair)))
> >        (loop for v being the hash-value in h do (print v))))

What you really want (I think) is
  (list "bar" 1)
instead of
  `("bar" 1)
(same with ("foo" 1)).

--
Matthieu Villeneuve