Next: , Previous: Symbol Variables, Up: Symbols


21.6.4 Operations Related to Symbols

Given any Scheme value, you can determine whether it is a symbol using the symbol? primitive:

— Scheme Procedure: symbol? obj
— C Function: scm_symbol_p (obj)

Return #t if obj is a symbol, otherwise #f. (r5rs)

Once you know that you have a symbol, you can obtain its name as a string by calling symbol->string. Note that Guile differs by default from R5RS on the details of symbol->string as regards case-sensitivity:

— Scheme Procedure: symbol->string symbol
— C Function: scm_symbol_to_string (symbol)

Return 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. (r5rs)

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"

Most symbols are created by writing them literally in code. However it is also possible to create symbols programmatically using the following string->symbol and string-ci->symbol procedures:

— Scheme Procedure: string->symbol string
— C Function: scm_string_to_symbol (string)

Return 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")
          ⇒ 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
— Scheme Procedure: string-ci->symbol str
— C Function: scm_string_ci_to_symbol (str)

Return the symbol whose name is str. str is converted to lowercase before the conversion is done, if Guile is currently reading symbols case-insensitively.

The following examples illustrate Guile's detailed behaviour as regards the case-sensitivity of symbols:

     (read-enable 'case-insensitive)   ; R5RS compliant behaviour
     
     (symbol->string 'flying-fish)    ⇒ "flying-fish"
     (symbol->string 'Martin)         ⇒ "martin"
     (symbol->string
        (string->symbol "Malvina"))   ⇒ "Malvina"
     
     (eq? 'mISSISSIppi 'mississippi)  ⇒ #t
     (string->symbol "mISSISSIppi")   ⇒ mISSISSIppi
     (eq? 'bitBlt (string->symbol "bitBlt")) ⇒ #f
     (eq? 'LolliPop
       (string->symbol (symbol->string 'LolliPop))) ⇒ #t
     (string=? "K. Harper, M.D."
       (symbol->string
         (string->symbol "K. Harper, M.D."))) ⇒ #t
     
     (read-disable 'case-insensitive)   ; Guile default behaviour
     
     (symbol->string 'flying-fish)    ⇒ "flying-fish"
     (symbol->string 'Martin)         ⇒ "Martin"
     (symbol->string
        (string->symbol "Malvina"))   ⇒ "Malvina"
     
     (eq? 'mISSISSIppi 'mississippi)  ⇒ #f
     (string->symbol "mISSISSIppi")   ⇒ mISSISSIppi
     (eq? 'bitBlt (string->symbol "bitBlt")) ⇒ #t
     (eq? 'LolliPop
       (string->symbol (symbol->string 'LolliPop))) ⇒ #t
     (string=? "K. Harper, M.D."
       (symbol->string
         (string->symbol "K. Harper, M.D."))) ⇒ #t

Finally, some applications, especially those that generate new Scheme code dynamically, need to generate symbols for use in the generated code. The gensym primitive meets this need:

— Scheme Procedure: gensym [name [obarray]]
— C Function: scm_gensym (name, obarray) |0 |2 |0

Create a new, unique symbol in obarray, using the global symbol table by default. If name is specified, it is used as a prefix for the new symbol's name. The default prefix is %%gensym. name may be either a string or a symbol.