Prev Up Next
The simple data types of Scheme include booleans, numbers,
characters, and symbols.
2.1.1 Booleans
Scheme's booleans are #t for true and #f for false.
Scheme has a predicate procedure called boolean? that
checks if its argument is boolean.
(boolean? #t) => #t
(boolean? "Hello, World!") => #f
The procedure not negates its argument, considered as a
boolean.
(not #f) => #t
(not #t) => #f
(not "Hello, World!") => #f
The last expression illustrates a Scheme convenience:
In a context that requires a boolean, Scheme will treat
any value that is not #f as a true value.
2.1.2 Numbers
Scheme numbers can be integers (eg,
42), rationals
(
22/7), reals (
3.1416), or complex (
2+3i). An
integer is a rational is a real is a complex number is a
number. Predicates exist for testing the various kinds of
numberness:
(number? 42) => #t
(number? #t) => #f
(complex? 2+3i) => #t
(real? 2+3i) => #f
(real? 3.1416) => #t
(real? 42) => #t
(rational? 3.1416) => #f
(rational? 22/7) => #t
(integer? 22/7) => #f
(integer? 42) => #t
Scheme integers need not be specified in decimal (base 10)
format. They can be specified in binary by prefixing the
numeral with #b. Thus #b1100 is the number twelve.
The octal prefix is #o and the hex prefix is
#x. (The optional decimal prefix is #d.)
Numbers can tested for equality using the general-purpose
equality predicate eqv?.
(eqv? 42 42) => #t
(eqv? 42 #f) => #f
(eqv? 42 42.0) => #f
However, if you know that the arguments to be compared are
numbers, the special number-equality predicate = is more
apt.
(= 42 42) => #t
(= 42 #f) -->ERROR!!!
(= 42 42.0) => #t
Other number comparisons are also possible:
(< 3 2) => #f
(>= 4.5 3) => #t
Arithmetic procedures +, -, *, / have the
expected behavior:
(+ 1 2 3) => 6
(- 5.3 2) => 3.3
(- 5 2 1) => 2
(* 1 2 3) => 6
(/ 2 3) => 1
This is just the tip of the iceberg. Scheme provides a
large and comprehensive suite of arithmetic and
trigonometric procedures. Consult R5RS KCR98.
2.1.3 Characters
Scheme character data are represented by prefixing the
character with #\. Thus,
#\c is the character
c. Some non-graphic characters have more descriptive
names, eg,
#\newline,
#\tab. The character for
space can be written
#\ , or more readably,
#\space.
The character predicate is char?:
(char? #\c) => #t
(char? 1) => #f
(char? #\;) => #t
Note that a semicolon character datum does not trigger
a comment.
The character data type has its set of comparison
predicates:
(char=? #\a #\a) => #t
(char<? #\a #\b) => #t
(char>=? #\a #\b) #\A
2.1.4 Symbols
The simple data types we saw above are
self-evaluating. Ie, if you typed any object from these
data types to the listener, the evaluated result returned by
the listener will be the same as what you typed in.
#t => #t
42 => 42
#\c => #\c
Symbols don't behave the same way. This is because symbols
are used by Scheme programs as identifiers for variables, and thus will evaluate to the value that the
variable holds. Nevertheless, symbols are a simple data
type, and symbols are legitimate values that Scheme can
traffic in, along with characters, numbers, and the rest.
To specify a symbol without making Scheme think it is a
variable, you should quote the symbol:
(quote xyz)
=> xyz
Since this type of quoting is very common in Scheme, a
convenient abbreviation is provided. The expression
'E
will be treated by Scheme as equivalent to
(quote E)
Scheme symbols are named by a sequence of characters. About
the only limitation on a symbol's name are that it shouldn't
be mistakable for some other data, eg, characters or booleans
or numbers or compound data. Thus, this-is-a-symbol,
i18n,
<=>, and $!#* are all symbols; 16, -i (a
complex number!), #t, "this-is-a-string", and
(barf) (a list) are not. The predicate for
checking symbolness is called symbol?:
(symbol? 'xyz) => #t
(symbol? 42) => #f
Scheme symbols are normally case-insensitive. Thus the
symbols
Calorie and calorie are identical:
(eqv? 'Calorie 'calorie)
=> #t
We can use the symbol xyz as a global variable by using
the form define:
(define xyz 9)
This says the variable xyz holds the value 9. If we
feed xyz to the listener, the result will be the value
held by xyz:
xyz
=> 9
We can use the form set! to change the value held by a
variable:
(set! xyz #\c)
Now
xyz
=> #\c
Up Next