[plt-scheme] Help with symbols
The use of symbols is like that of immutable strings with the big difference
that two symbols written in the same way always are equal in the sense of
*eq?* (as long as no uninterned symbols are around) Symbols can be used for
enumerated types, for example:
(define day-name->day-number day-name)
(case day-name
((Monday) 1)
((Tuesday) 2)
((Wednesday) 3)
((Thurstday) 4)
((Friday) 5)
((Saturday) 6)
((Sunday) 7)))
Notice that the day-names in the case statement are not evaluated, they are
taken as immediate data.
For the inverse we can write:
(define (day-number->day-name day-number)
(string-ref '(Monday Tuesday Wednesday Thurstday Friday Saturday Sunday)
(- day-number 1)))
(day-name->day-number (quote Friday)) --> 5
(day-number->day-name 5) --> Friday (a symbol)
Here the day-names can be regarded as an enumerated type.
An arrow signifies that evaluation of the lefthand side results in the
righthand side.
Notice that in `(day-name->day-number (quote Friday))' symbol `Friday' is
quoted.
This expression is quite different from:
(day-name->day-number Friday) --> error because variable `Friday' is not
bound.
When evaluating the latter, Scheme interprets the symbol `Friday' as the
name of a variable (which in the above code has not been bound and therefore
causes an error) The names of variables are usually called `identifiers'
because they identify storage locations containing values. Now, if we first
define:
(define Friday (quote Friday))
then we have:
(day-name->day-number Friday) --> 5
When evaluating the line `(define Friday (quote Friday))', symbol `Friday'
is used both as an identifier that refers to a storage location and as a
value to be stored at that location.
When evaluating the line `(day-name->day-number Friday)' the token `Friday'
is used as an identifier and its value is the symbol `Friday'.
Because symbols (and more generally symbolic expressions) are data and
programs are written as symbolic expressions, it is relatively easy to write
procedures that can manipulate programs, for example an interpreter for a
simple subset of Scheme (making a *good* interpreter/compiler for all of
Scheme is quite another thing) If we would like to write a C-compiler in C,
then we would have to read the C-program as a sequence of characters and do
all of the token parsing. In Scheme the tokens are already parsed by a
simple read (or by read-syntax)
Because symbols in a program text can denote both the identifiers of
variables and symbols used as data, symbols must be quoted when used as data
(usually, for example in the above case statement, syntax case does the
quoting implicitly)
A number needs no quotation because it cannot refer to any other thing than
itself. But when inserting a list in a program to be used as data, then this
list must be quoted, for otherwise it would be interpreted as code to be
evaluated.
(write 5) --> writes 5
(write (quote 5)) --> writes 5
(write (+ 2 3)) --> writes 5
(write (quote (+ 2 3))) --> writes (+ 2 3)
Proper use of quotation is not self evident, particularly (as in Scheme)
when the quoted fragments have the same appearence as the language used to
talk about the quotations. It was until the start of the twentiest century
that even some famous mathematiciens sometimes confused the use and the
mention of a word.
mention: `letter' is a word, not a letter.
use: I send you this letter and hope it is usefull.
Jos
----- Original Message -----
From: "Grant Rettke" <grettke at acm.org>
To: "PLT List" <plt-scheme at list.cs.brown.edu>
Sent: Sunday, February 15, 2009 5:38 PM
Subject: [plt-scheme] Help with symbols
> Hi,
>
> I have a question about symbols in Scheme.
>
> (symbol? '5) => #f
>
> Is this the case because 5 is self-evaluating and for lack of a better
> term, "5 is 5"?
>
> (symbol? '(+ 1 2)) =? #f
>
> Is this the case because it is a quoted list, and not a "name"?
>
> I see that I don't know much more about symbols other than they are a
> quoted sequence of characters about which I am not sure of the
> restrictions. Is there more to know about them? Where?
>
> Best wishes,
>
> Grant
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme