Node:Symbols, Next:Characters, Previous:Pairs and lists, Up:Other data types
Symbols are objects whose usefulness rests on the fact that two
symbols are identical (in the sense of eqv?
) if and only if their
names are spelled the same way. This is exactly the property needed to
represent identifiers in programs, and so most
implementations of Scheme use them internally for that purpose. Symbols
are useful for many other applications; for instance, they may be used
the way enumerated values are used in Pascal.
The rules for writing a symbol are exactly the same as the rules for writing an identifier; see sections Identifiers and Lexical structure.
It is guaranteed that any symbol that has been returned as part of
a literal expression, or read using the read
procedure, and
subsequently written out using the write
procedure, will read back
in as the identical symbol (in the sense of eqv?
). The
string->symbol
procedure, however, can create symbols for
which this write/read invariance may not hold because their names
contain special characters or letters in the non-standard case.
Note: Some implementations of Scheme have a feature known as "slashification" in order to guarantee write/read invariance for all symbols, but historically the most important use of this feature has been to compensate for the lack of a string data type.Some implementations also have "uninterned symbols", which defeat write/read invariance even in implementations with slashification, and also generate exceptions to the rule that two symbols are the same if and only if their names are spelled the same.
symbol? obj | procedure |
Returns #t if obj is a symbol, otherwise returns #f.
(symbol? 'foo) ==> #t (symbol? (car '(a b))) ==> #t (symbol? "bar") ==> #f (symbol? 'nil) ==> #t (symbol? '()) ==> #f (symbol? #f) ==> #f |
symbol->string symbol | procedure |
Returns the name of symbol as a string. If the symbol was part of
an object returned as the value of a literal expression
(section see Literal expressions) or by a call to the The following examples assume that the implementation's standard case is
lower case:
(symbol->string 'flying-fish) ==> "flying-fish" (symbol->string 'Martin) ==> "martin" (symbol->string (string->symbol "Malvina")) ==> "Malvina" |
string->symbol string | procedure |
Returns the symbol whose name is string. This procedure can
create symbols with names containing special characters or letters in
the non-standard case, but it is usually a bad idea to create such
symbols because in some implementations of Scheme they cannot be read as
themselves. See The following examples assume that the implementation's standard case is
lower case:
(eq? 'mISSISSIppi 'mississippi) ==> #t (string->symbol "mISSISSIppi") ==> the symbol with name "mISSISSIppi" (eq? 'bitBlt (string->symbol "bitBlt")) ==> #f (eq? 'JollyWog (string->symbol (symbol->string 'JollyWog))) ==> #t (string=? "K. Harper, M.D." (symbol->string (string->symbol "K. Harper, M.D."))) ==> #t |