6

I'm curious why:

(equal (make-symbol "foo") (make-symbol "foo"))

ends up beeing nil.

While

(equal 'foo 'foo)

is t.

Do i have a misconception of symbols? Aren't they just strings without double-quotes?

lordnik22
  • 121
  • 7

2 Answers2

13

Do i have a misconception of symbols? Aren't they just strings without double-quotes?

You do indeed have a misconception.

A symbol is a lisp object containing numerous properties (including its name, its variable/value slot; and its function slot; but you can in fact set arbitrary properties on them, and many symbols have more than just the above).

A symbol (object) can be referred to in code with a textual name (in which case the lisp reader interns that text to obtain the canonical symbol of that name); so you will usefully improve your understanding of Lisp if you understand that a symbol is not its name, but rather that the name is just a way of accessing a more complicated object1.

Furthermore, it is entirely valid (and useful) to be able to have more than one symbol with a given name, and make-symbol facilitates this by returning a new, un-interned symbol. Therefore it is expected and necessary that (equal (make-symbol "foo") (make-symbol "foo")) is nil, as you are comparing two independent objects (both of which happen to have the name "foo" -- and neither of which is the canonical symbol foo).

However most of the time you do actually want to obtain the single, canonical symbol of a given name, and this is what intern gives you:

ELISP> (eq 'foo (intern "foo"))
t

For more details, see C-hig (elisp)Creating Symbols RET


1 Beyond this you will at some point want to understand what the lisp reader is doing in general, and the different things which are being processed in the "read" vs "eval" phases of lisp execution. In brief, the "read" phase converts the code (text) you write into lisp objects, and the "eval" phase works on those objects (rather than on the text that you wrote). You don't necessarily need to know this, but certain things may fall into place more easily later on if you're at least vaguely aware of this distinction.

phils
  • 48,657
  • 3
  • 76
  • 115
6

You're confusing make-symbol and intern. make-symbol is used specifically when you want such an equality not to hold.

Stefan
  • 26,154
  • 3
  • 46
  • 84