Character strings in Scheme are written between double quotes. For example,
suppose we want an object that represents the text "Hello world!"
We can just write that in a program, in between double quotes:
"Hello, world!"
.
You can use a string as an expression--the value of a string is the string itself, just as the value of an integer is the integer itself. Like numeric literals and booleans, strings are "self-evaluating," which just means that if you have an expression in your program that consists of just a string, Scheme assumes you mean the value to be literally that string. There's nothing deep about this--it just turns out to be handy, because it makes it easy to use strings as literals.
Try typing the string "Hello, world."
at the Scheme prompt.
Scheme>"Hello, world!" "Hello, world!"
What happened here is that Scheme recognized the sequence of characters between double quotes as a string literal. The value of a literal string expression (in double quotes) is a (pointer to) a string object. A string object is a normal first-class object like a pair or a number, conceptually like an array that can only hold characters.
This value is what scheme printed out. The standard printed representation of a string object is the sequence of characters, with double quotes around it.
So what happened here is that Scheme read the sequence of characters in double quotes, constructed an array-like object of type string, then printed out the printed representation of that object.
If you want to print out a string, but without the double quotes, you
can use the standard procedure display
. If you pass display
a string, it just prints out the characters in the string, without any
double quotes.
display
is useful in programs that print information out for
normal users. Another useful procedure is newline
, which prints
a newline character, ending a line and starting a new one.
Try typing a (display "Hello, world!") (newline)
at the Scheme prompt.
What you get may look like this:
Scheme>(display "Hello, world!") (newline) Hello, world! #void
You might see something slightly different on your screen, depending
on the return value of newline
, which is unspecified in
the Scheme standard.
If you type in an expression using a string literal like "foo"
at the Scheme prompt, Scheme may construct a new string object with
that character sequence each time.
Try this:
Scheme>(define foo1 "foo") #void Scheme>(define foo2 "foo") #void Scheme>foo1 "foo" Scheme>foo2 "foo" Scheme>(eq? foo1 foo2) #f Scheme>(equal? foo1 foo2) #t
For each of the define
forms, Scheme has constructed a string
with the character sequence f
-o
-o
, and saved it
in a new variable binding. When we ask the value of each variable,
Scheme prints out the usual text representation of the string. The
printed representations are the same, since each string has the same
structure, but they're two different objects--when we ask if they're
eq?
, i.e., the very same object, the answer is no (#f
).
It's possible that in your system the eq?
comparison will
return #t
, because Scheme implementations are allowed to
use pointers to the same string if you type in two strings with
the same character sequence. For that reason, you should be careful
not to depend on whether Scheme strings are eq?
; you should
only distinguish whether they're equal?
. You can also use
the predicate string-equal?
if you know the arguments are
supposed to be strings. This has the advantage of signaling an
error if the arguments are of unexpected type.
Strings can be used as one-dimensional arrays (vectors) of characters. There are procedures for accessing their elements by an integer index, extracting substrings given two indices, and so on.