Can you use any string of characters as a variable? What are the rules?
Most Scheme variables are made up of letters, hyphens, and digits:
sum, days-in-week, stack-23.
However, the programmer can also, if she likes, freely incorporate any of
the characters !$%&*+./:<=>?~_^ into variables,
subject to the restriction that a variable may not begin with a plus sign,
a hyphen or minus sign, a digit, or a dot (that is, a period). The
restriction makes it easier for the Scheme processor to distinguish
variables from numerals.
Three sequences of characters that would otherwise be excluded by this last
restriction are specifically enumerated as permissible in the Scheme
standard: + (a plus sign standing alone), - (a
minus sign standing alone), and ... (three dots in immediate
succession). As a matter of style, the programmer should avoid using any
of these three variables for her own purposes.
The characters ()[]{};,"`'#\ must not be used in
variables.
So any string of characters that meets the restrictions above counts as a variable?
Almost. Nineteen specific sequences of characters are reserved for use as ``syntactic keywords,'' which are structural parts of certain kinds of expressions. Unlike variables, keywords cannot have bindings and do not stand for values. In fact, they don't stand for anything by themselves. They are not expressions, only constituents of larger expressions; they're more like the parentheses in a procedure call.
The nineteen syntactic keywords are:
=>,
and,
begin,
case,
cond,
define,
do,
else,
if,
lambda,
let,
let*,
letrec,
or,
quasiquote,
quote,
set!,
unquote, and
unquote-splicing.
An identifier is a sequence of characters that is either a variable or a syntactic keyword.
Does it make any difference whether I use lower-case letters or capitals?
Are quotient and QUOTIENT the same variable or
different?
Identifiers in Scheme are case-insensitive, so it doesn't matter whether
you spell them with lower-case letters, capital letters, or some
combination of cases. QUOTIENT is the same variable as
quotient, and IF is the same keyword as
if. (But check your local documentation carefully; this
rule is not all that popular with implementers of Scheme, and some of them
violate it rather casually.)
Next topic
Previous topic
Table of contents
This document is available on the World Wide Web as
http://www.math.grin.edu/~stone/scheme-web/identifiers.html