Next: , Previous: Pairs and lists, Up: Other data types

6.3.3 Symbols

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.

— procedure: symbol? obj

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
— procedure: symbol->string symbol

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 read procedure, and its name contains alphabetic characters, then the string returned will contain characters in the implementation's preferred standard case—some implementations will prefer upper case, others lower case. If the symbol was returned by string->symbol, the case of characters in the string returned will be the same as the case in the string that was passed to string->symbol. It is an error to apply mutation procedures like string-set! to strings returned by this procedure.

The following examples assume that the implementation's standard case is lower case:

(symbol->string 'flying-fish)
                                            ==>  "flying-fish"
     (symbol->string 'Martin)               ==>  "martin"
        (string->symbol "Malvina"))
                                            ==>  "Malvina"
— procedure: string->symbol string

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 symbol->string.

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
            (symbol->string 'JollyWog)))
               ==>  #t
     (string=? "K. Harper, M.D."
                 (string->symbol "K. Harper, M.D.")))
               ==>  #t