Scheme symbols
last modified November 24, 2026 Guile 3.0
A symbol is a name used as a value. Symbols look like identifiers,
but instead of standing for a variable they stand for themselves. The
data types article introduced them briefly.
This article explains why symbols can be compared with eq?, how
to convert between symbols and strings, and how programs use symbols as
enumeration values, keys, and tags. The examples run with GNU Guile 3.0.7.
Symbols as values
A symbol is written as a quoted identifier, such as 'green.
Without the quote, Scheme would evaluate green as a variable.
symbol? tests for a symbol.
(define colour 'green) (write colour) (newline) (display colour) (newline) (write (list (symbol? colour) (symbol? "green") (symbol? 'x))) (newline) (write (list 'Bratislava 'bratislava 'list->vector 'set-x! 'a.b '+)) (newline) (write (eq? 'Bratislava 'bratislava)) (newline) (write (list 'lambda 'if (quote define))) (newline)
write and display print a symbol without the quote,
because the quote belongs to the source code, not to the value. Symbols may
contain the same characters as identifiers, so list->vector,
set-x!, a.b, and + are all valid
symbols. Symbols are case-sensitive: 'Bratislava and
'bratislava differ. Even the names of special forms become plain
symbols when they are quoted, which is how programs can treat code as data.
$ guile names.scm green green (#t #f #t) (Bratislava bratislava list->vector set-x! a.b +) #f (lambda if define)
Symbol identity
Scheme keeps a table of all symbols it has seen. When the reader or
string->symbol meets a name, it returns the existing symbol
with that spelling, or adds a new one to the table. This is called
interning. As a result, two symbols with the same spelling are the
same object and can be compared with the fast eq?.
(define (make-name a b) (string-append a b)) (define s1 (make-name "sun" "flower")) (define s2 (make-name "sun" "flower")) (write (list (eq? s1 s2) (equal? s1 s2) (string=? s1 s2))) (newline) (define y1 (string->symbol s1)) (define y2 (string->symbol s2)) (write (list y1 y2)) (newline) (write (list (eq? y1 y2) (eq? y1 'sunflower))) (newline) (define flowers (list 'tulip 'rose 'sunflower 'lily)) (write (memq y1 flowers)) (newline) (write (memq (string->symbol "Rose") flowers)) (newline)
make-name builds two strings at run time. They have the same
characters, so equal? and string=? return true,
but they are two separate objects, and eq? returns false. The
symbols made from these strings are the same object, which is also the
symbol that the reader created for 'sunflower in the source.
That is why memq, which compares with eq?, finds the
symbol in a list. The search for Rose fails because the list
contains the lowercase rose.
$ guile identity.scm (#f #t #t) (sunflower sunflower) (#t #t) (sunflower lily) #f
Converting symbols and strings
symbol->string returns the name of a symbol as a string, and
string->symbol returns the symbol with a given name. Together
they let a program apply string operations to symbols.
(write (symbol->string 'planet))
(newline)
(write (string->symbol "moon"))
(newline)
(define (shout sym)
(string->symbol (string-upcase (symbol->string sym))))
(write (shout 'warning))
(newline)
(write (list (string->symbol "two words")
(string->symbol "42")
(string->symbol "")))
(newline)
(display (string->symbol "two words"))
(newline)
(write (symbol? (string->symbol "42")))
(display " ")
(write (number? (string->symbol "42")))
(newline)
(write '#{hello there}#)
(display " ")
(write (string-length (symbol->string '#{hello there}#)))
(newline)
shout converts a symbol to a string, changes the case with
string-upcase, and converts the result back.
string->symbol accepts any string, even one that could not be
read as a symbol: a name with a space, a name that looks like a number, or
the empty string. Guile prints such symbols in its #{...}#
notation, with both write and display, and the same
notation can be used in source code. The symbol named "42" is not a number.
$ guile converting.scm
"planet"
moon
WARNING
(#{two words}# #{42}# #{}#)
#{two words}#
#t #f
#{hello there}# 11
Symbols as enumeration values
A set of symbols serves well as an enumeration: a fixed list of names for
states, levels, or kinds. The names are readable when printed, and the
conditionals article showed that
case dispatches on them directly.
(define levels (list 'debug 'info 'warning 'error))
(define (valid-level? level)
(if (memq level levels) #t #f))
(define (level-rank level)
(case level
((debug) 0)
((info) 1)
((warning) 2)
((error) 3)
(else -1)))
(define (log-message level text threshold)
(when (>= (level-rank level) (level-rank threshold))
(display "[")
(display level)
(display "] ")
(display text)
(newline)))
(write (map valid-level? (list 'info 'fatal 'error)))
(newline)
(log-message 'debug "opening config file" 'info)
(log-message 'info "server started" 'info)
(log-message 'warning "disk 91% full" 'info)
(log-message 'error "cannot bind port 80" 'info)
levels lists the allowed log levels, and
valid-level? checks a symbol against them with
memq. level-rank maps each level to a number so that
levels can be compared. log-message prints a message only when
its level is at least the threshold, so the debug message is suppressed.
display prints the level symbol inside the brackets.
$ guile enums.scm (#t #f #t) [info] server started [warning] disk 91% full [error] cannot bind port 80
Symbols as keys
Symbols are the usual keys of association
lists. They are compared with eq?, so assq
finds them quickly, and they read like field names.
(define city
(list (cons 'name "Košice")
(cons 'country "Slovakia")
(cons 'population 228000)
(cons 'founded 1230)))
(define (field record key)
(let ((entry (assq key record)))
(if entry (cdr entry) 'unknown)))
(write (field city 'name))
(newline)
(write (field city 'population))
(newline)
(write (field city 'mayor))
(newline)
(for-each (lambda (key)
(display key)
(display ": ")
(display (field city key))
(newline))
(list 'name 'country 'founded))
(write (map car city))
(newline)
city describes a city with four symbol keys. field
looks up a key with assq and returns the symbol
unknown when the key is missing. The loop prints selected fields
with their names, and (map car city) lists all the keys of the
record.
$ guile keys.scm "Košice" 228000 unknown name: Košice country: Slovakia founded: 1230 (name country population founded)
Building symbols
Programs that generate names, such as code generators, often build symbols
from parts. Guile's symbol-append joins symbols directly; the
portable way is to convert to strings, join them, and convert back.
(write (symbol-append 'make- 'point)) (newline) (write (symbol-append 'point '- 'x)) (newline) (define (accessor-name type field) (symbol-append type '- field)) (define (accessor-names type fields) (map (lambda (field) (accessor-name type field)) fields)) (write (accessor-names 'book (list 'title 'author 'year))) (newline) (define (numbered prefix n) (string->symbol (string-append (symbol->string prefix) (number->string n)))) (write (map (lambda (i) (numbered 'reg i)) (iota 4))) (newline) (write (eq? (numbered 'reg 2) 'reg2)) (newline)
accessor-name joins a type name, a hyphen, and a field name into
names like book-title. numbered appends a number to
a prefix with string-append and number->string,
which works in any Scheme. Because the result is interned,
(numbered 'reg 2) is the same object as the literal
'reg2.
$ guile building.scm make-point point-x (book-title book-author book-year) (reg0 reg1 reg2 reg3) #t
Unique symbols
Sometimes a program needs a symbol that differs from every symbol the user
could write. gensym creates a fresh symbol with a generated name,
optionally with a given prefix. make-symbol creates an
uninterned symbol, which is never put into the symbol table, so
no other symbol is eq? to it, even one with the same name.
(define a (gensym))
(define b (gensym "node-"))
(define c (gensym "node-"))
(write (list a b c))
(newline)
(write (list (symbol? b) (eq? b c)))
(newline)
(define missing (make-symbol "missing"))
(write (symbol->string missing))
(newline)
(write (list (eq? missing 'missing) (symbol-interned? missing)))
(newline)
(define (lookup key alist)
(let ((entry (assq key alist)))
(if entry (cdr entry) missing)))
(define settings (list (cons 'debug #f) (cons 'colour 'missing)))
(for-each (lambda (key)
(let ((value (lookup key settings)))
(display key)
(display (if (eq? value missing) " not set" " is set"))
(newline)))
(list 'debug 'colour 'font))
Each gensym call returns a new symbol; the numbers come from a
counter and may differ between Guile versions and programs. The uninterned
missing is named "missing", but it is not eq? to the
symbol 'missing. That makes it a safe marker for a value that is
not present: lookup returns it for an absent key, and the caller
can tell the absent font from colour, whose value
happens to be the ordinary symbol missing, and from
debug, whose value is #f.
$ guile unique.scm
(#{ g18}# node-19 node-20)
(#t #f)
"missing"
(#f #f)
debug is set
colour is set
font not set
Symbols as tags
A list whose first element is a symbol is a simple way to represent tagged data. The symbol says what kind of value the rest of the list holds, and procedures dispatch on it.
(define (make-circle r) (list 'circle r))
(define (make-rectangle w h) (list 'rectangle w h))
(define (make-triangle b h) (list 'triangle b h))
(define (shape-kind shape) (car shape))
(define (area shape)
(case (shape-kind shape)
((circle) (let ((r (cadr shape))) (* 3.14159 r r)))
((rectangle) (* (cadr shape) (caddr shape)))
((triangle) (/ (* (cadr shape) (caddr shape)) 2))
(else #f)))
(define shapes
(list (make-circle 1)
(make-rectangle 3 4)
(make-triangle 6 5)
(make-circle 2)))
(for-each (lambda (shape)
(display (shape-kind shape))
(display " ")
(display (area shape))
(newline))
shapes)
(write (length (filter (lambda (s) (eq? (shape-kind s) 'circle)) shapes)))
(newline)
The three constructors put a tag in front of the dimensions of a shape.
area reads the tag with shape-kind and chooses a
formula with case; for an unknown tag it returns
#f. The triangle area is an exact integer because the base
and height are exact. The last lines count the circles by comparing tags with
eq?. The records article shows a
sturdier way to define such types.
$ guile tagged.scm circle 3.14159 rectangle 12 triangle 15 circle 12.56636 2
Keywords
Guile has a related type, the keyword, written with a
#: prefix. Keywords evaluate to themselves and are used to name
arguments, as the procedures article showed
with define*. They can be converted to and from symbols.
(define k #:width) (write k) (newline) (write (list (keyword? k) (symbol? k) (keyword? 'width))) (newline) (write (keyword->symbol k)) (display " ") (write (symbol->keyword 'height)) (newline) (write (eq? (symbol->keyword 'width) #:width)) (newline) (define* (box #:key (width 1) (height 1)) (* width height)) (define (call-box sizes) (apply box sizes)) (write (box #:width 3 #:height 4)) (newline) (write (call-box (list (symbol->keyword 'height) 5))) (newline)
A keyword is neither a symbol nor equal to the symbol with the same name.
keyword->symbol and symbol->keyword convert
between the two, and keywords are interned like symbols, so
eq? compares them. call-box uses
apply with a keyword that was built from a symbol at run time,
which shows that keyword arguments are ordinary values in the argument list.
$ guile keywords.scm #:width (#t #f #f) width #:height #t 12 5
Source
Symbols — GNU Guile Reference Manual
Keywords — GNU Guile Reference Manual
In this article we have quoted symbols, compared them with eq?
thanks to interning, converted them to and from strings, used them as
enumeration values, keys, and tags, built new symbols from parts, created
unique symbols with gensym and make-symbol, and
converted between symbols and Guile keywords. The
booleans and equality article compares the
equality predicates, and the association
lists article uses symbol keys throughout.
Author
List all Scheme tutorials.