3 ms·
Shapiro does a good job of covering packages, probably better than most if not all, other books. Because I started in Lisp with that book (back in late 1999 or
by kazinator 3y ago
Shapiro does a good job of covering packages, probably better than most if not all, other books.
Because I started in Lisp with that book (back in late 1999 or 2000? I can't remember), I was never confused about how packages and symbols work.
What the book does right is explain, early, that symbols are objects that have a name which is a character string, and which is different from the token syntax so that Foo and FOO and |FOO| are the same symbol:
Quote from the Symbol chapter:
> Returning to our discussion of symbols, every symbol has a print name, or
simply a name, which is a string of the characters used to print the symbol.
The name of the symbol frank is the string "FRANK". You can see the name
of a symbol by using the function symbol-name
After that it goes into how we can get any characters we want into a symbol name, and so on.
Then, early in the Packages chapter:
> In this chapter, you will see that several different symbols can have the same
symbol name—as long as they are in different packages.
Thanks to starting with this book, I had an accurate mental model of packages from the get go.
The "name spaces for symbols" phrase appears only in the Preface. Firstly, nobody should be taking a passing remark in the Preface as a lecture on how packages work. Secondly, the phrase has a straightforward interpretation which is squarely correct. A symbol has a name which is a string. That name exists in a namespace, which is a package. If we understand "name" to mean "symbol name" (and not "symbol used as a name") then a package is a namespace, and it is of symbols.
In that sense, a lexical scope is a namespace for variables and function bindings, and a class object contains a namespace for slots. Those namespaces are not for symbols, they are based on symbols being the names; i.e of symbols.
If you don't know anything about Lisp symbols and packages, and are reading only the Preface, you will probably not understand what exactly "name spaces for symbols" means; you will just have to read the book, and carefully go through the Symbols and Packages chapters.
- lisper 3y ago> Foo and FOO and |FOO| are the same symbol No, they aren't. The strings "Foo" and "FOO" and "|FOO|" might be read by the reader as the same symbol, but then again, they might not. It depends on a great many things. By default, all else being equal, yes, reading these three strings will produce the same symbol. But there are any number of factors that can change this. Clozure Common Lisp Version 1.12.1 (v1.12.1-10-gca107b94) DarwinX8664 ? (setf x 'foo) FOO [Stuff elided] ? (eq x 'foo) NIL [More stuff elided] ? (EQ 'foo 'FOO) NIL Figuring out what I left out is left as an (elementary) exercise (though the fact that I had to type EQ instead of eq in the last line is a big clue). And I know that you know this. My point is not that you don't understand symbols or name spaces; I know you do. My point is that explaining this stuff is hard, and very few people seem to be willing to put in the effort. And this is not unique to CL, and it's not even unique to software, or even to STEM. It seems to be endemic in the human condition. But that doesn't mean one should not lament it or try to improve it.
- kazinator 3y agoIt's probably not necessary to mention that the treatment of case and whatnot is default behavior. That can be covered in an advanced chapter about read tables. There we can say, oh we lied when we said that foo, FOO and |FOO| read the same; this is actually highly programmable. The student will not encounter that unless they explore someone else's code, or discover the features like readtable-case and experiment. Just talking about the default behavior, if it is stable and portable, and doesn't mysteriously flip behind your back, is fine.
- lisper 3y agoEven if you leave case out of it, it is not true that Foo and FOO are necessarily the same symbol, or even that FOO and FOO are necessarily the same symbol. In fact, the whole point of packages is that you can have two different symbols with the same name.