From: Oli Schwarz
Subject: type of hash-keys
Date: 
Message-ID: <cdosln$hea$1@online.de>
Hello,

here is a little function.

(defun show-elem()
 (let
  ((myhash (make-hash-table)))
  (setf (gethash 'foo myhash) "Hello")
  (setf (gethash "bar" myhash) "World")
  (setf (gethash 0 myhash) "Zero")
  (print (gethash 'foo myhash))
  (print (gethash "bar" myhash))
  (print (gethash 0 myhash))
 )
 )

And thats the output of the function:

"Hello" 
NIL 
"Zero" 
3 

Why I cant get the value to the key "bar". It seems that 0 and the quoted
key 'foo work correct. And there are 3 entrys in the table.

Are Strings unavailable as keys in hash-tables?

I'm new to lisp. Maybe thats the problem :)

I tried that in Clisp:

Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
Copyright (c) Bruno Haible, Sam Steingold 1999-2000
Copyright (c) Sam Steingold, Bruno Haible 2001-2004


bye Oli

From: Edi Weitz
Subject: Re: type of hash-keys
Date: 
Message-ID: <87zn5sj6oy.fsf@bird.agharta.de>
On Thu, 22 Jul 2004 17:14:32 +0000 (UTC), Oli Schwarz <·····@gmx.net> wrote:

> (defun show-elem()
>  (let
>   ((myhash (make-hash-table)))
>   (setf (gethash 'foo myhash) "Hello")
>   (setf (gethash "bar" myhash) "World")
>   (setf (gethash 0 myhash) "Zero")
>   (print (gethash 'foo myhash))
>   (print (gethash "bar" myhash))
>   (print (gethash 0 myhash))
>  )
>  )
>
> And thats the output of the function:
>
> "Hello" 
> NIL 
> "Zero" 
> 3 
>
> Why I cant get the value to the key "bar". It seems that 0 and the
> quoted key 'foo work correct. And there are 3 entrys in the table.

No, 4.

> Are Strings unavailable as keys in hash-tables?

No, the reason is that the default test function for hash tables is
EQL and two strings usually aren't EQL even if they have the same
characters. Use EQUAL as your test function.

  * (eql "bar" "bar")

  NIL
  * (equal "bar" "bar")

  T
  * (let ((hash (make-hash-table)))
      (setf (gethash "bar" hash) 42)
      (gethash "bar" hash))

  NIL
  NIL
  * (let ((hash (make-hash-table :test #'equal)))
      (setf (gethash "bar" hash) 42)
      (gethash "bar" hash))

  42
  T
  * (let ((hash (make-hash-table)) 
          (string "bar"))
      (setf (gethash string hash) 42)
      (list (gethash "bar" hash) 
            (gethash string hash)))

  (NIL 42)

Note that in the last example the string STRING which we use when
storing the value is /identical/ to the string STRING we use to
retrieve the value. The other string "bar" isn't.

Check these links for more info about hash tables:

  <http://www.gigamonkeys.com/book/collections.html>
  <http://www.lispworks.com/reference/HyperSpec/Body/18_.htm>
  <http://cl-cookbook.sourceforge.net/hashes.html>

Cheers,
Edi.

-- 

"Lisp doesn't look any deader than usual to me."
(David Thornley, reply to a question older than most languages)

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: Edi Weitz
Subject: Re: type of hash-keys
Date: 
Message-ID: <87vfggj6l6.fsf@bird.agharta.de>
On Thu, 22 Jul 2004 19:24:45 +0200, Edi Weitz <········@agharta.de> wrote:

> No, 4.

Sorry, 3 of course.

My math prof would be proud of me... :)
From: Oli Schwarz
Subject: Re: type of hash-keys
Date: 
Message-ID: <cdpamk$5na$1@online.de>
Edi Weitz <········@agharta.de> schrieb:
> On Thu, 22 Jul 2004 17:14:32 +0000 (UTC), Oli Schwarz <·····@gmx.net> wrote:
>
> No, the reason is that the default test function for hash tables is
> EQL and two strings usually aren't EQL even if they have the same
> characters. Use EQUAL as your test function.

Thanks, that it was.

Oli