When you type in a string, e.g., "This here is a string, you know."
,
you can type in pretty much whatever you want, as long as it's between double
quotes and doesn't have double quotes or nonprinting characters in the middle.
(You can have strings with double quotes in them, but you have to use a special
escape sequence trick.)
When you type in a symbol, on the other hand, you have to
be a little more careful--some character sequences count as symbol
names, but others don't. For example, the character sequence
1
2
3
doesn't count as a symbol 123
,
because it's a number. Character sequences with spaces, parentheses,
and single quotes in them are also a no-no, because those characters
have special meaning when reading and writing the printed representations
of Scheme data structures.
A symbol name has to start with an "extended alphabatic" character--that
a letter or any of a fairly large set of printing characters, followed by a
string of other extended alphabetic characters or digits.
(The extended alphabetic characters are a
-z
, A
-Z
,
and these: + - . * / < = > ! ? : $ % _ & ~ ^
.)
For example, the following are all symbols:
x
thursdays-total*3
am_is_are_was_were_be_being_been
able-was-I-ere-I-saw-elba
floppy_drive-3.5
fourscore-and-7-years-ago
x-15+three-times-thirty-seven
=1
lhs=>rhs
x+/-3%
There is a slight restriction that you can't use a symbol name
that starts with a character that could begin a literal number.
This includes not only digits, but +
, -
, .
and #
. A special exception to this is that +
, and
-
, by themselves, are symbols, and so is ...
(the ellipsis identifier used in macros).
Scheme identifiers (variable names and special form names and keywords) have almost the same restrictions as Scheme symbol object character sequences, and it's no coincidence. Most implementations of Scheme happen to be written in Scheme, and symbol objects are used in the interpreter or compiler to represent variable names.
Don't read too much into this, however: it's easy to write a Scheme interpreter or compiler in Scheme, and that is why the rules for symbol names are the same as the rules for variable names, but symbols and variables are very, very different things. A symbol is just a data object, like a string, that has the special property of being unique. You can use symbols like any other data object, as part of any data structure.
It just happens that interpreters and compilers generally use symbol objects to represent the names of variables and whatnot, so it's convenient that the rules for symbol object names are the same as the rules for identifiers in the language--but there is no other connection.
Symbols are not necessarily variable names, they're just a kind of data object (like strings) that happen to get used that way, by some programs (interpreters and compilers). Your programs can use them any way you choose. (Sorry to be repetitive on this point, but confusing symbols and variables is one of the most common and avoidable problems in learning Scheme. It's worse in Lisp, where symbols and variables do have a deep connection, but not an obvious one.)