From: Jason
Subject: key values for gethash
Date: 
Message-ID: <1145145519.964714.40440@i40g2000cwc.googlegroups.com>
I have just finished "ANSI Common Lisp" by Paul Graham. :)

Looking through the latter parts of the book, I see that he calls
gethash with both a quoted key, and a key prefaced with a colon. This
confuses me. At first I assumed there was a type-o, but when I tested
the code it works as expected.

So, my question is: what is the significant difference between the
following gethash calls?

[8]> (setf jason (make-hash-table))
#S(HASH-TABLE EQL)
[9]> (gethash 'whatever jason)
NIL ;
NIL
[10]> (setf (gethash 'whatever jason) '(a beer))   ; <<< (1) THIS CALL
USES A QUOTE
(A BEER)
[11]> (gethash 'whatever jason)
(A BEER) ;
T
[12]> (setf (gethash :whatever jason) '(a coke)) ; << (2) THIS CALL
USES A COLON
(A COKE)
[13]> (gethash 'whatever jason)
(A BEER) ;
T
[14]> (gethash :whatever jason)
(A COKE) ;
T

Thanks,

-Jason

From: Ari Johnson
Subject: Re: key values for gethash
Date: 
Message-ID: <m2hd4ugx8i.fsf@hermes.theari.com>
"Jason" <·······@gmail.com> writes:

> I have just finished "ANSI Common Lisp" by Paul Graham. :)
>
> Looking through the latter parts of the book, I see that he calls
> gethash with both a quoted key, and a key prefaced with a colon. This
> confuses me. At first I assumed there was a type-o, but when I tested
> the code it works as expected.

The following should answer the question for you.  Paying attention to
what 'whatever and :whatever actually mean syntactically will fill in
any blanks that are left.

CL-USER> (describe 'whatever)
Symbol: WHATEVER
INTERNAL in package: #<Package "COMMON-LISP-USER">
Print name: "WHATEVER"
Value: #<Unbound>
Function: #<Unbound>
Plist: NIL
; No value
CL-USER> (describe :whatever)
Symbol: :WHATEVER
Constant
EXTERNAL in package: #<Package "KEYWORD">
Print name: "WHATEVER"
Value: :WHATEVER
Function: #<Unbound>
Plist: NIL
; No value
CL-USER> (eq 'whatever :whatever)
NIL
CL-USER> (eq (symbol-package 'whatever) (symbol-package :whatever))
NIL
CL-USER> (eq (symbol-name 'whatever) (symbol-name :whatever))
T
From: Jason
Subject: Re: key values for gethash
Date: 
Message-ID: <1145150447.492606.150880@i39g2000cwa.googlegroups.com>
Ari Johnson wrote:
> "Jason" <·······@gmail.com> writes:
>
> > I have just finished "ANSI Common Lisp" by Paul Graham. :)
> >
> > Looking through the latter parts of the book, I see that he calls
> > gethash with both a quoted key, and a key prefaced with a colon. This
> > confuses me. At first I assumed there was a type-o, but when I tested
> > the code it works as expected.
>
> The following should answer the question for you.  Paying attention to
> what 'whatever and :whatever actually mean syntactically will fill in
> any blanks that are left.
>
> CL-USER> (describe 'whatever)
> Symbol: WHATEVER
> INTERNAL in package: #<Package "COMMON-LISP-USER">
> Print name: "WHATEVER"
> Value: #<Unbound>
> Function: #<Unbound>
> Plist: NIL
> ; No value
> CL-USER> (describe :whatever)
> Symbol: :WHATEVER
> Constant
> EXTERNAL in package: #<Package "KEYWORD">
> Print name: "WHATEVER"
> Value: :WHATEVER
> Function: #<Unbound>
> Plist: NIL
> ; No value
> CL-USER> (eq 'whatever :whatever)
> NIL
> CL-USER> (eq (symbol-package 'whatever) (symbol-package :whatever))
> NIL
> CL-USER> (eq (symbol-name 'whatever) (symbol-name :whatever))
> T

This tells me that 'whatever is the name of a symbol that is variable,
and in interned in the COMMON-LISP USER package, whereas :whatever is
the the name of a symbol that is constant, and in exported from the
package KEYWORD. Very interesting...

Now, what is the significance of this? For example, if I am using hash
tables as part of my program, why would I ever choose to use :whatever
as a hash value?

-Jason
From: Ken Tilton
Subject: Re: key values for gethash
Date: 
Message-ID: <A5h0g.376$Jw2.186@fe10.lga>
Jason wrote:
> Ari Johnson wrote:
> 
>>"Jason" <·······@gmail.com> writes:
>>
>>
>>>I have just finished "ANSI Common Lisp" by Paul Graham. :)
>>>
>>>Looking through the latter parts of the book, I see that he calls
>>>gethash with both a quoted key, and a key prefaced with a colon. This
>>>confuses me. At first I assumed there was a type-o, but when I tested
>>>the code it works as expected.
>>
>>The following should answer the question for you.  Paying attention to
>>what 'whatever and :whatever actually mean syntactically will fill in
>>any blanks that are left.
>>
>>CL-USER> (describe 'whatever)
>>Symbol: WHATEVER
>>INTERNAL in package: #<Package "COMMON-LISP-USER">
>>Print name: "WHATEVER"
>>Value: #<Unbound>
>>Function: #<Unbound>
>>Plist: NIL
>>; No value
>>CL-USER> (describe :whatever)
>>Symbol: :WHATEVER
>>Constant
>>EXTERNAL in package: #<Package "KEYWORD">
>>Print name: "WHATEVER"
>>Value: :WHATEVER
>>Function: #<Unbound>
>>Plist: NIL
>>; No value
>>CL-USER> (eq 'whatever :whatever)
>>NIL
>>CL-USER> (eq (symbol-package 'whatever) (symbol-package :whatever))
>>NIL
>>CL-USER> (eq (symbol-name 'whatever) (symbol-name :whatever))
>>T
> 
> 
> This tells me that 'whatever is the name of a symbol that is variable,
> and in interned in the COMMON-LISP USER package, whereas :whatever is
> the the name of a symbol that is constant, and in exported from the
> package KEYWORD. Very interesting...

No need for exporting when it comes to the keyword package, but I know 
what you mean.

> 
> Now, what is the significance of this? For example, if I am using hash
> tables as part of my program, why would I ever choose to use :whatever
> as a hash value?

So code in different packages can get to the same value without 
specifying the package of the symbol used as a key. Such bugs are always 
fun to track down, especially if one is not used to worrying about 
symbol packages.

ken

-- 
Cells: http://common-lisp.net/project/cells/

"Have you ever been in a relationship?"
    Attorney for Mary Winkler, confessed killer of her
    minister husband, when asked if the couple had
    marital problems.
From: Jason Meade
Subject: Re: key values for gethash
Date: 
Message-ID: <m2psjimgc3.fsf@socrates.local>
Ken Tilton <·········@gmail.com> writes:

> Jason wrote:
>> Ari Johnson wrote:
>> 
>>>"Jason" <·······@gmail.com> writes:
>>>
>>>
>>>>I have just finished "ANSI Common Lisp" by Paul Graham. :)
>>>>
>>>>Looking through the latter parts of the book, I see that he calls
>>>>gethash with both a quoted key, and a key prefaced with a colon. This
>>>>confuses me. At first I assumed there was a type-o, but when I tested
>>>>the code it works as expected.
>>>
>>>The following should answer the question for you.  Paying attention to
>>>what 'whatever and :whatever actually mean syntactically will fill in
>>>any blanks that are left.
>>>
>>>CL-USER> (describe 'whatever)
>>>Symbol: WHATEVER
>>>INTERNAL in package: #<Package "COMMON-LISP-USER">
>>>Print name: "WHATEVER"
>>>Value: #<Unbound>
>>>Function: #<Unbound>
>>>Plist: NIL
>>>; No value
>>>CL-USER> (describe :whatever)
>>>Symbol: :WHATEVER
>>>Constant
>>>EXTERNAL in package: #<Package "KEYWORD">
>>>Print name: "WHATEVER"
>>>Value: :WHATEVER
>>>Function: #<Unbound>
>>>Plist: NIL
>>>; No value
>>>CL-USER> (eq 'whatever :whatever)
>>>NIL
>>>CL-USER> (eq (symbol-package 'whatever) (symbol-package :whatever))
>>>NIL
>>>CL-USER> (eq (symbol-name 'whatever) (symbol-name :whatever))
>>>T
>> This tells me that 'whatever is the name of a symbol that is
>> variable,
>> and in interned in the COMMON-LISP USER package, whereas :whatever is
>> the the name of a symbol that is constant, and in exported from the
>> package KEYWORD. Very interesting...
>
> No need for exporting when it comes to the keyword package, but I know
> what you mean.
>
>> Now, what is the significance of this? For example, if I am using
>> hash
>> tables as part of my program, why would I ever choose to use :whatever
>> as a hash value?
>
> So code in different packages can get to the same value without
> specifying the package of the symbol used as a key. Such bugs are
> always fun to track down, especially if one is not used to worrying
> about symbol packages.
>
> ken
>

That makes sense. Thanks. I can see (theoretically) why this would be important
but my experience with Lisp is so minor, that I'm having difficulty coming
up with a good example to illustrate this...

This highlights one of the troubles that I've been having with this book. Paul
throws things like this into his examples, but neglects to note why they are
there. For a beginner like me, seeing (gethash :parent our-circle) followed
by (gethash 'radius our-circle) without a footnote was just a little bit 
too much.

-Jason
From: Thomas A. Russ
Subject: Re: key values for gethash
Date: 
Message-ID: <ymibqv0xhab.fsf@sevak.isi.edu>
Actually, this opens up a debate about what actually is the best method
to use for symbolic constants in code.

Keywords have the nice property that they are in their own package, are
in fact constants, and don't need to be quoted.  That makes them easy to
use across different packages without having to know (or at least
specify) which package you mean.  Keywords have the drawback that you
could potentially have name collisions between different intended uses
of the symbols.  In practice this will often be rare, because the code
using the keywords would have to access the same data structures (hash
tables in your examples) but intend different meanings.  This is more
likely if you have some sort of global resource that many other programs
would be using.

Symbols in the implementation package would have the same issues as
keywords as far as name collisions go.

Symbols in the using program's package would avoid the name collision
problem.  They would need to be quoted, and if you have a large system
with multiple packages, you would need to establish a convention for
determining which of the program packages to use for the constants.
This gives the most flexibility, but also requires a bit more up-front
planning and design.

In general, I prefer to use keywords for symbolic constants, simply
because the syntax for specifying them makes it clear that it is a
symbolic constant that is being used, but my decision could change if
the circumstances dictate.

-Tom.

Hey, that would be a long footnote ;)

-- 
Thomas A. Russ,  USC/Information Sciences Institute
From: Peter Seibel
Subject: Re: key values for gethash
Date: 
Message-ID: <m2mzek58jk.fsf@gigamonkeys.com>
···@sevak.isi.edu (Thomas A. Russ) writes:

> In general, I prefer to use keywords for symbolic constants, simply
> because the syntax for specifying them makes it clear that it is a
> symbolic constant that is being used, but my decision could change
> if the circumstances dictate.

FWIW, I have a rule of thumb that says, use keywords if you only need
one namespace but use non-keyword symbols if you may need multiple
namespaces. For instance, if you're defining an extensible framework
where folks can define their own commands, commands should be named
with non-keyword symbols because then you can just use the package
system to manage the namespace of commands written by different
authors. But if you're implementing a fixed set of symbolic constants
that are given meaning by your code then you can use keywords.

-Peter

-- 
Peter Seibel           * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp  * http://www.gigamonkeys.com/book/