6.8. Symbols


6.8. Symbols

Symbols are used for a variety of purposes as symbolic names in Scheme programs. Strings could be used for most of the same purposes, but an important characteristic of symbols makes comparisons between symbols much more efficient. This characteristic is that two symbols with the same name are identical in the sense of eq?. The reason is that the Scheme reader (see read in Section 7.1) and the procedure string->symbol catalog symbols in an internal symbol table and always return the same symbol whenever the same name is encountered. Thus, no character-by-character comparison is needed, as would be needed to compare two strings.

The property that two symbols may be compared quickly for equivalence makes them ideally suited for use as identifiers in the representation of programs, allowing fast comparison of identifiers. This property also makes symbols useful for a variety of other purposes. For example, symbols might be used as messages passed between procedures, labels for list-structured records, or names for objects stored in an association list (see assq in Section 6.3).

Symbols are written without double quotes or other bracketing characters. Parentheses, double quotes, spaces, and most other characters with a special meaning to the Scheme reader are not allowed within the printed representation of a symbol. Some implementations, however, support the use of backward slashes to escape special characters occurring in symbols, in a manner similar to the use of backward slashes in strings.

Refer to Section 1.1 or the formal syntax of Scheme at the back of this book for a precise description of the syntax of symbols.

(string->symbol string)

procedure

returns: a symbol whose name is string

string->symbol records all symbols it creates in an internal table that it shares with the system reader, read. If a symbol whose name is equivalent to string (according to the predicate string=?) already exists in the table, this symbol is returned. Otherwise, a new symbol is created with string as its name; this symbol is entered into the table and returned.

The system reader arranges to convert all symbols to a single case (lower case is assumed in this book), before entering them into the internal table. string->symbol does not. Thus, it is possible to produce symbols in lower case, upper case, or even mixed case, using string->symbol. It is also possible to create symbols with names that contain special characters, such as spaces or parentheses.

 (string->symbol "x")  x (eq? (string->symbol "x") 'x)  #t (eq? (string->symbol "X") 'x)  #f (eq? (string->symbol "x")      (string->symbol "x"))  #t 

(symbol->string symbol)

procedure

returns: a string, the name of symbol

The string returned by symbol->string for a symbol created by an earlier call to string->symbol may or may not be the same string (by eq?) as the string passed to string->symbol. That is, an implementation is free to copy or not to copy a string it uses as the name of a symbol. Unpredictable behavior can result if a string passed to string->symbol is altered with string-set! or by any other means.

 (symbol->string 'xyz)  "xyz" (symbol->string (string->symbol "Hi"))  "Hi" (symbol->string (string->symbol "()"))  "()" 




The Scheme Programming Language
The Scheme Programming Language
ISBN: 026251298X
EAN: 2147483647
Year: 2003
Pages: 98

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net