You’ve just encountered a variety of types in Scheme, including not only many numeric types (integers, reals, rationals, etc.), but also strings, which are used to represent texts of various sorts. In our tour of the basic types used in Scheme, we should consider one more: symbols.
Scheme’s ancestor, Lisp, was originally developed to aid in experiments in artificial intelligence. At the time, a leading theory suggested that intelligence emphasizes symbolic manipulation. Hence, it is sensible that Lisp and Scheme include symbols as a basic type. Evidence also shows that many programs most appropriately work on abstract symbolic value.
So, what is a symbol? A symbol is simply a word (usually) that we use to denote only itself. Unlike a variable, it has no associated value. Symbols are also atomic, we cannot split them apart (as we might a string, which we can split into individual characters). The primary operation we perform on symbols is comparison (determining whether two symbols are the same). We can’t even compare two symbols for order. (Should jelly come before or after jam? Who decides?)
When we want to refer to something as a value involved in a computation, rather than as the name of some other value, we put an apostrophe (usually pronounced “quote”) in front of it. In effect, by quoting the symbol, we’re telling Scheme to take it literally and without further interpretation or evaluation:
> 'sample 'sample
We can also create symbols using the
quote operation. (In fact,
the apostrophe is a shorthand for “use
> (quote sample) 'sample
Note: The apostrophe and the quote operation only turn words and word-like structures into symbols. If you provide them with, say, a list or a number, they behave somewhat differently. A quoted number is just that number. A quoted list is a list containing quoted versions of each element (more or less).
What good are symbols? It turns out that the design of Lisp (and Scheme,
and Racket) is such that it’s much faster to compare symbols than to
compare strings. That means that we often use symbols when we want to
provide a mnemonic value to a procedure. For example, you’ve seen us
'solid parameter to the
For the first part of the semester, we will primarily use symbols in this fashion. Later in the semester, we may consider other approaches to using symbols.
'sample (with the quote) is very different from
(without the quote). In the first case, Scheme interprets it as a
symbol (an atomic value). In the second, Scheme interprets it as
an identifier that names another value (e.g., something defined
define). At first, you may find the distinction a bit
confusing. However, as you get used to programming in Scheme, the
distinction will become natural.
> (define sample 85) > 'sample 'sample > sample 85 > (list sample) '(85) > '(sample) '(sample)
'solid as a symbol we’ve used already. Identify
a few others.
Copyright © Charlie Curtsinger, Sarah Dahlby Albright, Janet Davis, Nicole Eikmeier, Fahmida Hamid, Titus Klinge, Peter-Michael Osera, Samuel A. Rebelsky, Anya Vostinar, and Jerod Weinman. Selected materials are copyright by John David Stone or Henry Walker and are used with permission.
Unless specified otherwise elsewhere on this page, this work is licensed under a Creative Commons Attribution 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/ or send a letter to Creative Commons, 543 Howard Street, 5th Floor, San Francisco, California, 94105, USA.