CSC151.02 2010S Functional Problem Solving : Readings
Primary: [Front Door] [Schedule] - [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] - [Assignment] [Quiz]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Quizzes] [Readings]
References: [A-Z] [By Topic] - [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [CSC151.01 2010S (Weinman)] [CSC151 2009F (Rebelsky)]
Misc: [SamR] [MediaScript] [GIMP]
Summary: In this reading, we consider one of Scheme's central kinds of values, symbols.
While your initial exercises in Scheme have been numeric, Scheme is not limited to numerical computation, but can also operate on pure 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 sequence of letters). The primary operation we perform on symbols is comparison (determining whether two symbols are the same).
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.
>
(quote sample)
sample
So, what can you do with symbols? Not a whole lot. You can determine
if a value is a symbol using the symbol?
procedure and
you can determine if two symbols are the same using the equal?
procedure. Note that Scheme uses #t
for “yes” on
#f
for “no”. We'll return to those value when we
begin to consider conditionals.
>
(symbol? 'sample)
#t
>
(symbol? 23)
#f
>
(equal? 'sample 'elpmas)
#f
>
(equal? 'sample 'sample)
#t
Note that 'sample
(with the quote) is very different
from sample
(without the quote). In the first case,
Scheme interprets it as a symbol (an atomic value). In the second,
Scheme interprets it as a name for another value (e.g., something
defined with 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
>
(symbol? 'sample)
#t
>
(symbol? sample)
#f
>
(equal? sample 'sample)
#f
The problem becomes even worse when you use a name as a symbol, and the name has not been defined.
>
(symbol? 'elpmas)
#t
>
(symbol? elpmas)
reference to undefined identifier: elpmas
Note that
does
not create a symbol. Rather, it tells Scheme
to take something literally. Hence, you can can quote many different
things. For example, you can even quote Scheme expressions.
quote
>
'(+ 2 3)
(+ 2 3)
>
(quote (* 2 3))
(* 2 3)
Although you can use quote in a variety of ways, we prefer that you limit your use to quoting symbolic values, at least for the first few weeks of class. Our experience shows that those who quote other kinds of values early in the course end up with confusing results later in the course.
Primary: [Front Door] [Schedule] - [Academic Honesty] [Instructions]
Current: [Outline] [EBoard] [Reading] [Lab] - [Assignment] [Quiz]
Groupings: [Assignments] [EBoards] [Examples] [Exams] [Handouts] [Labs] [Outlines] [Projects] [Quizzes] [Readings]
References: [A-Z] [By Topic] - [Scheme Report (R5RS)] [R6RS] [TSPL4]
Related Courses: [CSC151.01 2010S (Weinman)] [CSC151 2009F (Rebelsky)]
Misc: [SamR] [MediaScript] [GIMP]
Copyright (c) 2007-10 Janet Davis, Matthew Kluber, Samuel A. Rebelsky, and Jerod Weinman. (Selected materials copyright by John David Stone and Henry Walker and used by permission.)
This material is based upon work partially supported by the National Science Foundation under Grant No. CCLI-0633090. Any opinions, findings, and conclusions or recommendations expressed in this material are those of the author(s) and do not necessarily reflect the views of the National Science Foundation.
This work is licensed under a Creative Commons
Attribution-NonCommercial 2.5 License. To view a copy of this
license, visit http://creativecommons.org/licenses/by-nc/2.5/
or send a letter to Creative Commons, 543 Howard Street, 5th Floor,
San Francisco, California, 94105, USA.